Advertisement

C++ quirk, let's see if you get it...

Started by December 13, 2000 01:39 PM
11 comments, last by felisandria 24 years, 1 month ago
I''m compiling, I''m bored, so here''s a little thing my boss brought up yesterday. I thought it was interesting. What''s the difference between: Type *y = new Type(); Type *y = new Type; Yes there''s a difference, apparently... c''mon gurus. *grin* -fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Argh. I give up. Maybe it''s because I''m new to C++. *sigh*. Is it an easy question fel? Or is it meant to be a killer?

Phil
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Advertisement
Aww, only one person even cares? hehe.

The difference is that
Type *y = new Type;
returns a pointer to a Type object, whereas, in MSVC at least,
Type *y = new Type();
returns a function pointer to the constructor of a Type object. Now, while these resolve to the same thing at the end of the constructor, if you attempt to do recursion inside the constructor (which would be how we found it) you''re in deep trouble if you use Type *y = new Type();
So, only use ()''s when doing news if you''re calling something other than the default constructor.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Actually I cared, but seeing as I had no clue what the difference was I figured if I replied saying "I have no clue" that people would think that I didn''t have a clue.

Oh wait... DAMN!


- Houdini
- Houdini
That''s freakin awesome.
Yet another C++ quirk to remember .

I didn''t reply cause I had absolutely no idea besides the fact that it had something to do with the default constructor. I love these kind of posts. I want more of them little puzzles.

Btw, I do not quite understand how the functionpointer to the default constructor resolves to a pointer to a new object itself,

and how can that go wrong with recursion inside the constructor? Besides, when the h*ck do you need recursion in the constructor for? Could you give an example of that?

Thanks,

Jaap Suter

____________________________
Mmmm, I''''ll have to think of one.
____________________________Mmmm, I''ll have to think of one.
Yeah, that one got me, too..

I am forced to wonder, however, if that''s just a MSVC quirk, or if it''s officially what it''s supposed to do.

I''d check in g++ if i weren''t so lazy, and if i trusted g++ to be fully standards compliant to such a degree.

I guess we may never know.

-ben.c
Advertisement
I believe what Felisandria means is that the generic function new has a single memory address at runtime. This means if your type has private members of 3 strings initialized char * string1= new char [50]. /etc the same function new that was called for your type has been called inside of its definition, as long as you haven''t written a default constructor, which would have a different memory address. Now, I''m assuming this would overwrite your type (or at least part of it) with a string because they should occupy the same memory address.

--Could be wrong on all of this though.
I would have sed something totally different.
Well anyways, you learn something new everyday.
I admit it, I was stumped.

I also tried it without pointers:
Type t1;
Type t2 ();

The second line isn''t executed, but it doesn''t give an error. However, if you try to do t2.any_member (), it doesn''t recognize t2 as a Type.

So from this, I take it that the type for a pointer to the constructor for a class is the class name itself? Weird.

Fel-
Did your boss say when you''d ever USE this?
quote: Original post by felisandria
...snip...
Now, while these resolve to the same thing at the end of the constructor, if you attempt to do recursion inside the constructor (which would be how we found it) you''re in deep trouble if you use Type *y = new Type();
-fel


OK, fel, just noticed this. Uh...why are you doing recursion in the constructor? Just curious--some weird design pattern?

This topic is closed to new replies.

Advertisement