• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ Question

No, but remember that when using something like this, variable b is a char, not a pointer.

char* a, b;
 
nkgreen, generally you shouldn't declare variables on the same line (For reasons just like this).

I prefer the char* a over the char *a. To me, I'm defining a pointer variable, so the * is naturally part of the name. If I put the * on the name, it just looks like I'm trying to dereference an uninitialized variable.
 
Originally posted by: Cogman
nkgreen, generally you shouldn't declare variables on the same line (For reasons just like this).

I prefer the char* a over the char *a. To me, I'm defining a pointer variable, so the * is naturally part of the name. If I put the * on the name, it just looks like I'm trying to dereference an uninitialized variable.

Future languages should enforce this ......
 
I generally don't, but I have caught myself doing it before and making that mistake. It's just one of those things that was taught (not necessarily to use or not, just allowable), and slips in there sometimes. 😛
 
You want to know something I've done that's pretty bad? I made an array of a base class pointer and put classes derived from that base class into that array. It didn't work so well 🙂. It took me forever to discover what was actually going wrong, finally, It struck me "Oh, the array is being indexed by the size of the Base class, not the size of the derived class"

That had to be the most confusing error I've ever seen
 
That's perfectly legal, though.

Base* ba = new Base[3];
ba[0] = Derived();
ba[1] = Derived();
ba[2] = Derived();

The derived objects you construct will be "sliced" down into Base objects - that is, their type converts from Derived to Base. What isn't legal is this:

Derived* da = static_cast<Derived*>(ba);

For obvious reasons.
 
Originally posted by: Sc4freak
That's perfectly legal, though.

Base* ba = new Base[3];
ba[0] = Derived();
ba[1] = Derived();
ba[2] = Derived();

The derived objects you construct will be "sliced" down into Base objects - that is, their type converts from Derived to Base. What isn't legal is this:

Derived* da = static_cast<Derived*>(ba);

For obvious reasons.

Gah, sorry, I explained it wrong. This is what I did.

Derived* array = new Derived[size];
Base* ba = array;
ba[7];

While that will compile fine, it is not correct. (at least with gcc)
 
Gah, sorry, I explained it wrong. This is what I did.

Derived* array = new Derived[size];
Base* ba = array;
ba[7];

While that will compile fine, it is not correct. (at least with gcc)
My C++ isn't strong but it seems to me like that should work just fine? You're not allocating space on the stack for objects of that class, just pointers, which should be the same size regardless of what you're pointing to. Am I wrong?
 
My C++ isn't strong but it seems to me like that should work just fine? You're not allocating space on the stack for objects of that class, just pointers, which should be the same size regardless of what you're pointing to. Am I wrong?

The array stores Derived objects contiguously in memory on the heap. The compiler will compute the address of each element in the array by adding [sizeof(Derived) * index] to the base address of the array.

If you cast the array to an array of Base, the compiler will perform those calculations using sizeof(Base) instead. If Derived is larger than Base, the resulting address will be incorrect.
 
If you cast the array to an array of Base, the compiler will perform those calculations using sizeof(Base) instead. If Derived is larger than Base, the resulting address will be incorrect.

You are correct. As the poster said, he explained it wrong. He originally made it sound like he was doing this:

Code:
Base** array = new Base*[10];
for (int i=0 ; i < 10 ; ++i) array[i] = new Derived;

Which is perfectly valid, since all pointers are the same size regardless of type.
 
Back
Top