struct inheritance

N

Neill

Hello,

Can somebody explain why prog doesn't point to struct NumExp please?

Best Regards,
Neill
----------------------
struct Exp_ {};
typedef Exp_* Exp;
struct NumExp : public Exp_
{
int num;
NumExp(int n)
{
num = n;
}
};
int main()
{
Exp prog = new NumExp(10);
return 0;
}
 
K

Kai-Uwe Bux

Neill said:
Hello,

Can somebody explain why prog doesn't point to struct NumExp please?

Huh? How did you tell?
struct Exp_ {};
typedef Exp_* Exp;
struct NumExp : public Exp_
{
int num;
NumExp(int n)
{
num = n;
}
};
int main()
{
Exp prog = new NumExp(10);
return 0;
}

What did this program do (in terms of observable behavior) that you did not
expect?

If you run this program in a debugger and the debugger does not show the
correct dynamic type of *prog, then there could be a problem with your
debugger. In this case, you might want to consult the documentation of your
tool chain or contact the vendor. The C++ looks fine (except that you
should consider an initializer list for the constructor).


Best

Kai-Uwe Bux
 
N

Neill

The output is different than when writing:

NumExp* prog = new NumExp(10);

so maybe I should consider using gcc instead of Visual C++?
 
K

Kai-Uwe Bux

Please don't top post. That's the local custom.
The output is different than when writing:

NumExp* prog = new NumExp(10);

Uhm, the program that you posted has no output at all. Apparently, now you
are talking about some program that you did not post. Since my crystal ball
is currently in repair, I have no idea what you are talking about.
so maybe I should consider using gcc instead of Visual C++?

Well, changing the compiler is not supposed to affect the output.


And please trim stuff that you do not reference.


Best

Kai-Uwe Bux
 
N

Neill

Neill said:
Uhm, the program that you posted has no output at all. Apparently, now you
are talking about some program that you did not post. Since my crystal
ball
is currently in repair, I have no idea what you are talking about.

Srry, I mean, debugger is giving different results:

with: NumExp* prog = new NumExp(10);

prog --> some address
|_ Exp_ --> {...}
|_ num --> 10

with: Exp prog = new NumExp(10);

prog --> only some address but I would like to get same result as
above
 
I

Ian Collins

Neill said:
Srry, I mean, debugger is giving different results:

with: NumExp* prog = new NumExp(10);

prog --> some address
|_ Exp_ --> {...}
|_ num --> 10

with: Exp prog = new NumExp(10);

prog --> only some address but I would like to get same result as
above

Why? The second expression wouldn't compile. What are you really testing?
 
N

Neill

Ian Collins said:
Why? The second expression wouldn't compile. What are you really
testing?

The second expression compiles just fine over here with Visual Studio .NET
2003 so I'm all confused.. Note that: typedef Exp_* Exp;
I would like to get same result with 2nd expression as with 1st but
something is wrong with this code and I don't know what.

struct Exp_ {};
typedef Exp_* Exp;
struct NumExp : public Exp_
{
int num;
NumExp(int n) : num(n) {}
};
int main()
{
Exp prog = new NumExp(10);
return 0;
}

Best regards
 
K

Kai-Uwe Bux

Neill said:
Srry, I mean, debugger is giving different results:

with: NumExp* prog = new NumExp(10);

prog --> some address
|_ Exp_ --> {...}
|_ num --> 10

with: Exp prog = new NumExp(10);

prog --> only some address but I would like to get same result as
above

I think, the debugger might be using the static type information of the prog
variable to decide which output to print. In any case, this output seems to
be more related to the debugger than it is to your program. Rest assured
that

Exp prog = new NumExp( 10 );

will cause prog to point to a NumExp object (NumExp inheriting from Exp_ and
Exp being a typedef for Exp_*). It appears that the debugger is just not
showing dynamic type information.

I have no idea whether this would work, but you might add a virtual
destructor to Exp_. This way, you indicate to the debugger that Exp_ is a
polymorphic type. Maybe the debugger picks up on that.


Best
 
I

Ian Collins

Neill said:
The second expression compiles just fine over here with Visual Studio .NET
2003 so I'm all confused.. Note that: typedef Exp_* Exp;

One good reason not to use pointless typedefs!
I would like to get same result with 2nd expression as with 1st but
something is wrong with this code and I don't know what.

Yes but why do you want to get the same result?

What is the significance of the value?
 
N

Neill

Kai-Uwe Bux said:
I think, the debugger might be using the static type information of the
prog
variable to decide which output to print. In any case, this output seems
to
be more related to the debugger than it is to your program. Rest assured
that

That's right! I did a check with:

NumExp* test = new NumExp(10);
Exp prog = test;

test gives the expected debug info whereas prog does not..

Thanks for your advice!
Best regards
 
N

Neill

Ian Collins said:
One good reason not to use pointless typedefs!


Yes but why do you want to get the same result?

What is the significance of the value?

My intention was to inherit several different struct's from Exp_ so I could
treat them all the same. I was confused because the debugger is using static
type information.

NumExp* test = new NumExp(10);
Exp prog = test;

Debug information for test and prog is different.

Best regards
 
D

Daniel Pitts

Neill said:
My intention was to inherit several different struct's from Exp_ so I could
treat them all the same. I was confused because the debugger is using static
type information.

NumExp* test = new NumExp(10);
Exp prog = test;

Debug information for test and prog is different.

Best regards
NumExp* is a different static type than Exp_*, so you *will* have aa
different program.

try:
Exp_* test = new NumExp(10);
instead of
NumExp* test = new NumExp(10);
 
I

Ian Collins

Neill said:
My intention was to inherit several different struct's from Exp_ so I could
treat them all the same. I was confused because the debugger is using static
type information.

Well you can't "treat them all the same". They are different types.
You can cast between pointers to the base and derived types, but
instances of them are distinct types.
NumExp* test = new NumExp(10);
Exp prog = test;

Please, drop the confusing and pointless typedef.
Debug information for test and prog is different.

They are different types. Why are you so hung up on the debugger
information. Throw it away and concentrate on observed behaviour.
 
J

James Kanze

Huh? How did you tell?
What did this program do (in terms of observable behavior)
that you did not expect?
If you run this program in a debugger and the debugger does
not show the correct dynamic type of *prog, then there could
be a problem with your debugger.

Given a pointer to an Exp_, I don't know how the debugger can
possibly know that the object in question is actually a NumExp.
The types aren't polymorphic (which probably means that the
conversion of the pointer was an error).

If the intent is to use the types polymorphically, Exp_ must
have at least one virtual function. If the intent is to new
objects of the derived type dynamically, and assign the
resulting pointer to a pointer to Exp_, Exp_ almost certainly
needs a virtual destructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top