Tag Archive

The following is a list of all entries tagged with C/C++:

How To Prevent Creating Objects On Heap

Yesterday I saw a C++ interview question that was asking the interviewees to prevent the clients from creating objects on heap. That is, you write a class and your class users can’t use new to create an object.
Here is my solution:

1
2
3
4
5
6
7
8
9
10
11
class Foo
{
public:
Foo(const Foo& f) {}
static Foo getAnInstance()
[...]


Solving Cyclic Dependencies Is Hard

I am a C/C++ beginner but I’d been already taught that dependency cycles are generally evil since it’s very hard to solve. I haven’t realized how hard the problem is before, because all the works I’ve done are “one file” programs or I can just simply throw all the files into a single gcc/g++ command.
However, [...]


Weird Hex Output

In C++, to output and integer in hex, I’d been taught to do this:

1
cout << hex << an_integer;

And if I want to show “0x” before the value, I need to do the following:

1
2
cout.setf(ios::showbase);
cout << hex << an_integer;

The above code works well for either positive or negative integers, but not the value ZERO. If I run [...]