C++ Question

Kirby

Lifer
Apr 10, 2006
12,028
2
0
No, but remember that when using something like this, variable b is a char, not a pointer.

char* a, b;
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
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.
 
Sep 29, 2004
18,656
67
91
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 ......
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
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.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
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
 

Sc4freak

Guest
Oct 22, 2004
953
0
0
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.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
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)
 
Oct 27, 2007
17,009
1
0
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?
 

Venix

Golden Member
Aug 22, 2002
1,084
3
81
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.
 

pdusen

Member
May 8, 2008
39
0
0
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.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |