473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

initialization on a struct member fails

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

Jan 19 '07 #1
4 1666
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;
}

Jan 19 '07 #2
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 !!!

Jan 19 '07 #3
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.
Jan 19 '07 #4

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. :-(

Jan 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
10
by: emma middlebrook | last post by:
Hi I discovered that if you declare a structure (and not 'new()' it) you can then separately initialize its members and the compiler counts those separate statements as a full initialization....
14
by: gustavo | last post by:
I was looking at the Sendmail's source code, and i've got confused about this kind of initialization: ------------------------ struct prival PrivacyValues = { { "public", PRIV_PUBLIC }, {...
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
8
by: Sheldon | last post by:
Hi, Can anyone help with this problem with setting up nested structures and initializing them for use. I have created several structs and placed them in a super struct that I will then pass to...
5
by: ssylee | last post by:
I'm not sure if I can initialize members of a struct the lazy way. For example, let's say I have a struct defined as below: typedef struct _ABC { BOOL A; BOOL B; BOOL C; } ABC;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.