Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialization on a struct member fails

Reply
Thread Tools

initialization on a struct member fails

 
 
Peter Liu
Guest
Posts: n/a
 
      01-19-2007
This simple code

typedef strt B{
int b;
}BS;

typedef struct A:B
{
int a;
int foo(){return a+b;}
}AS;

int main()
{
AS*pab=new AS();
pab->b=4;
pab->a=5;
pab->a=pab->foo();

***********//Output result is incorrect only 4 //***********

delete pab;
}

Why isn't the output 9 as it is supposed to be ?

Thanks

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-19-2007
On Jan 19, 3:46 pm, "Peter Liu" <ppp1234567890123456...@yahoo.co.id>
wrote:
> This simple code
>
> typedef strt B{
> int b;
>
> }BS;typedef struct A:B
> {
> int a;
> int foo(){return a+b;}
>
> }AS;int main()
> {
> AS*pab=new AS();
> pab->b=4;
> pab->a=5;
> pab->a=pab->foo();
>
> ***********//Output result is incorrect only 4 //***********
>
> delete pab;
>
> }Why isn't the output 9 as it is supposed to be ?


By converting it to C++ it works much better:

#include <iostream>

struct B{
int b;
};

struct A : public B
{
int a;
int foo(){return a+b;}

};

int main()
{
A* pab = new A();
pab->b=4;
pab->a=5;
std::cout << pab->foo();

delete pab;
}

 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      01-19-2007
Peter Liu wrote:

> This simple code
>
> typedef strt B{

? strt

> int foo(){return a+b;}

It works ok
movl 8(%ebp), %eax
movl 4(%eax), %edx //b->%edx
movl 8(%ebp), %eax
movl (%eax), %eax //a->%eax
leal (%edx,%eax), %eax // a+b->%eax !!! look like add !!!

 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      01-19-2007
Peter Liu wrote:
> This simple code
>
> typedef strt B{
> int b;
> }BS;
>
> typedef struct A:B
> {
> int a;
> int foo(){return a+b;}
> }AS;
>
> int main()
> {
> AS*pab=new AS();
> pab->b=4;
> pab->a=5;
> pab->a=pab->foo();
>
> ***********//Output result is incorrect only 4 //***********
>
> delete pab;
> }
>
> Why isn't the output 9 as it is supposed to be ?


It is 9 on my system, after changing "strt" to
"struct" and adding an actual output statement.
That goes to show you haven't given us the actual
code that fails.

Also note that what you are doing is assignment,
not initialization.

- J.
 
Reply With Quote
 
Peter Liu
Guest
Posts: n/a
 
      01-19-2007

Jacek Dziedzic $B$N%a%C%;!<%8(B:
> Peter Liu wrote:
> > This simple code
> >
> > typedef strt B{
> > int b;
> > }BS;
> >
> > typedef struct A:B
> > {
> > int a;
> > int foo(){return a+b;}
> > }AS;
> >
> > int main()
> > {
> > AS*pab=new AS();
> > pab->b=4;
> > pab->a=5;
> > pab->a=pab->foo();
> >
> > ***********//Output result is incorrect only 4 //***********
> >
> > delete pab;
> > }
> >
> > Why isn't the output 9 as it is supposed to be ?

>
> It is 9 on my system, after changing "strt" to
> "struct" and adding an actual output statement.
> That goes to show you haven't given us the actual
> code that fails.
>
> Also note that what you are doing is assignment,
> not initialization.
>
> - J.


Sorry everyone, it works correctly, my mistake hehehhe.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
struct member initialization ssylee C Programming 4 08-22-2008 09:16 PM
static struct initialization in a Class:: -- not my struct christian.bongiorno@gmail.com C++ 2 09-20-2006 06:53 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57