class A {
public:
char text_a[100];
A() { *text_a=0; }
~A() {}
};
//-----------------------------------------------------------------------------
class B {
public:
char text_b[100];
B() { *text_b=0; }
~B() {}
};
//-----------------------------------------------------------------------------
class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; }
~C() {}
};
//-------------------------------------------------------------------------------
void test() {
B *bp1,*bp2;
C c,*cp1,*cp2,*cp 3;
void *p;
strcpy(c.text_a ,"hello a");
strcpy(c.text_b ,"hello b");
strcpy(c.text_c ,"hello c");
cp1=&c;
p=cp1;
bp1=cp1; // ok
bp2=(B*)p; // resulting bp2 is WRONG!
cp2=(C*)p; // ok
cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2
is already wrong.
}
//-----------------------------------------------------------------------------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
I can imagine someone saying "p is not pointing to a B object".
But if you think about it, conceptually, it does, imho.
So is it more a technical matter of compiling this!?
Maybe i'm stupid and/or missing something essential about C++.
If so, please give me a link to where i can study this right.
Cheers,
Jo 31 1922
On Jun 18, 11:17 am, Jo <jo.lan...@tele net.bewrote:
class A {
public:
char text_a[100];
A() { *text_a=0; }
~A() {}};
//-----------------------------------------------------------------------------
class B {
public:
char text_b[100];
B() { *text_b=0; }
~B() {}};
//-----------------------------------------------------------------------------
class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; }
~C() {}};
//-------------------------------------------------------------------------------
void test() {
B *bp1,*bp2;
C c,*cp1,*cp2,*cp 3;
void *p;
strcpy(c.text_a ,"hello a");
strcpy(c.text_b ,"hello b");
strcpy(c.text_c ,"hello c");
cp1=&c;
p=cp1;
bp1=cp1; // ok
bp2=(B*)p; // resulting bp2 is WRONG!
cp2=(C*)p; // ok
cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2
is already wrong.}
//-----------------------------------------------------------------------------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
I can imagine someone saying "p is not pointing to a B object".
But if you think about it, conceptually, it does, imho.
So is it more a technical matter of compiling this!?
Maybe i'm stupid and/or missing something essential about C++.
If so, please give me a link to where i can study this right.
Cheers,
Jo
I re-wrote your test function like this -
int main() {
B *bp1,*bp2;
C c,*cp1;
void *p;
int a;
strcpy(c.text_a ,"hello a");
strcpy(c.text_b ,"hello b");
strcpy(c.text_c ,"hello c");
cp1=&c;
p=cp1;
bp2=(B*)p;
std::cout<<p<<" \n";
std::cout<<bp2;
std::cin>>a;
}
I printed the value in p and also in bp2. Both are one and the same.
On my m/c the O/P was -
0x22fe28
0x22fe28
Why do you think, in your program, resulting bp2 is WRONG?
Bharath wrote:
>On Jun 18, 11:17 am, Jo <jo.lan...@tele net.bewrote:
>>class A {
public:
char text_a[100];
A() { *text_a=0; } ~A() {}};
//----------------------------------------------------------------------------- class B {
public:
char text_b[100];
B() { *text_b=0; } ~B() {}};
//----------------------------------------------------------------------------- class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; } ~C() {}};
//------------------------------------------------------------------------------- void test() {
B *bp1,*bp2; C c,*cp1,*cp2,*cp 3; void *p;
strcpy(c.text_a ,"hello a"); strcpy(c.text_b ,"hello b"); strcpy(c.text_c ,"hello c"); cp1=&c; p=cp1; bp1=cp1; // ok bp2=(B*)p; // resulting bp2 is WRONG! cp2=(C*)p; // ok cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2 is already wrong.}
//-----------------------------------------------------------------------------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
I can imagine someone saying "p is not pointing to a B object". But if you think about it, conceptually, it does, imho.
So is it more a technical matter of compiling this!?
Maybe i'm stupid and/or missing something essential about C++.
If so, please give me a link to where i can study this right.
Cheers,
Jo
I re-wrote your test function like this - int main() {
B *bp1,*bp2; C c,*cp1; void *p; int a;
strcpy(c.text_a ,"hello a");
strcpy(c.text_b ,"hello b");
strcpy(c.text_c ,"hello c");
cp1=&c;
p=cp1;
bp2=(B*)p;
std::cout<<p<<" \n";
std::cout<<bp2;
std::cin>>a; }
I printed the value in p and also in bp2. Both are one and the same. On my m/c the O/P was - 0x22fe28 0x22fe28
Why do you think, in your program, resulting bp2 is WRONG?
Because i got runtime problems.
So i narrowed down everything to the above code.
Running this on my PC gives a wrong bp2 pointer. I can see it in the
"Watch" panel.
And i also did a similar thing than you: at the end of the test/main
function i did a
OutputDebugStri ng(cp3->text_a);
OutputDebugStri ng(cp3->text_b);
OutputDebugStri ng(cp3->text_c);
This gives rubbish for text a!!!
Compilation is done with Visual C++ 7.1.3088
Jo wrote:
class A {
public:
char text_a[100];
A() { *text_a=0; }
~A() {}
};
//-----------------------------------------------------------------------------
class B {
public:
char text_b[100];
B() { *text_b=0; }
~B() {}
};
//-----------------------------------------------------------------------------
class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; }
~C() {}
};
//-------------------------------------------------------------------------------
void test() {
B *bp1,*bp2;
C c,*cp1,*cp2,*cp 3;
void *p;
strcpy(c.text_a ,"hello a");
strcpy(c.text_b ,"hello b");
strcpy(c.text_c ,"hello c");
cp1=&c;
p=cp1;
bp1=cp1; // ok
bp2=(B*)p; // resulting bp2 is WRONG!
cp2=(C*)p; // ok
cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2 is
already wrong.
}
//-----------------------------------------------------------------------------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
I can imagine someone saying "p is not pointing to a B object".
Right.
But if you think about it, conceptually, it does, imho.
No, it doesn't.
>
So is it more a technical matter of compiling this!?
No the code is wrong.
>
Maybe i'm stupid and/or missing something essential about C++.
I doubt you're stupid but you're certainly missing something essential.
>
If so, please give me a link to where i can study this right.
I have no idea why you think that code should be right, that is my problem.
Perhaps you are expecting an ordinary cast to work like a dynamic_cast?
When you write
bp2=(B*)p; // resulting bp2 is WRONG!
you are expecting the program to 'discover' that there a B object hidden
inside the object that p is pointing to?
If so google for dynamic_cast, or better still read a good book.
john
Jo <jo*******@tele net.bewrote in
news:be******** *************@p hobos.telenet-ops.be:
class A {
public:
char text_a[100];
A() { *text_a=0; }
~A() {}
};
//---------------------------------------------------------------------
-------- class B {
public:
char text_b[100];
B() { *text_b=0; }
~B() {}
};
//---------------------------------------------------------------------
-------- class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; }
~C() {}
};
//---------------------------------------------------------------------
---------- void test() {
B *bp1,*bp2;
C c,*cp1,*cp2,*cp 3;
void *p;
strcpy(c.text_a ,"hello a");
strcpy(c.text_b ,"hello b");
strcpy(c.text_c ,"hello c");
cp1=&c;
p=cp1;
bp1=cp1; // ok
bp2=(B*)p; // resulting bp2 is WRONG!
cp2=(C*)p; // ok
cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2
is already wrong.
}
//---------------------------------------------------------------------
--------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
Because you are casting a p back into a pointer type that it wasn't
originally. p was originally a C*. You need to cast it back into a C*
first.
I can imagine someone saying "p is not pointing to a B object".
But if you think about it, conceptually, it does, imho.
Nope. It may be pointing at an A obect.
So is it more a technical matter of compiling this!?
Maybe i'm stupid and/or missing something essential about C++.
If so, please give me a link to where i can study this right.
Standard says you can cast a pointer to object into a void*, but you can
only cast the void* back into the pointer type that it was originally.
Anything else is undefined behaviour.
<Implementati on-specific behaviour>
What's probably happening in your case is that the memory layout of your
object is A->B->C. So when you take your C* (which is effectively also
an A*, but is _not_ a B*...), hammer it into a void*, and then re-form it
as a B*, you are effectively casting an A* to a B*. Since they're
unrelated types, who knows what the operations on B do to an A object.
So.. you should cast that void* to a C* first, then assign the C* to a B*
(no cast required). The compiler will know at that point that you're
upcasting and will adjust the pointer value accordingly.
</Implementation-specific behaviour>
Andre Kostur wrote:
>Jo <jo*******@tele net.bewrote in news:be******* **************@ phobos.telenet-ops.be:
>>class A {
public:
char text_a[100];
A() { *text_a=0; } ~A() {} }; //--------------------------------------------------------------------- -------- class B {
public:
char text_b[100];
B() { *text_b=0; } ~B() {} }; //--------------------------------------------------------------------- -------- class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; } ~C() {} }; //--------------------------------------------------------------------- ---------- void test() {
B *bp1,*bp2; C c,*cp1,*cp2,*cp 3; void *p;
strcpy(c.text_a ,"hello a"); strcpy(c.text_b ,"hello b"); strcpy(c.text_c ,"hello c"); cp1=&c; p=cp1; bp1=cp1; // ok bp2=(B*)p; // resulting bp2 is WRONG! cp2=(C*)p; // ok cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2 is already wrong. } //--------------------------------------------------------------------- --------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
Because you are casting a p back into a pointer type that it wasn't originally. p was originally a C*. You need to cast it back into a C* first.
In the real code, i don't know yet what the type is.
I first need to get a B pointer, then i can deduce the target class.
Do i understand it right that this problem would not occur if
>>I can imagine someone saying "p is not pointing to a B object". But if you think about it, conceptually, it does, imho.
Nope. It may be pointing at an A obect.
That's technically speaking, not conceptually, imo.
If C is derived from A & B, then a C object IS an A and IS a B. At the
same time.
But i understand that at a certain point we must transition from
concepts into practical technology.
What i mean is: in practice: p cannot point to both the A and B instance
at the same time, so you're right about that.
So that's where we come into the implementation specifics.
>>So is it more a technical matter of compiling this!?
Maybe i'm stupid and/or missing something essential about C++.
If so, please give me a link to where i can study this right.
Standard says you can cast a pointer to object into a void*, but you can only cast the void* back into the pointer type that it was originally. Anything else is undefined behaviour.
<Implementatio n-specific behaviour> What's probably happening in your case is that the memory layout of your object is A->B->C. So when you take your C* (which is effectively also an A*, but is _not_ a B*...), hammer it into a void*, and then re-form it as a B*, you are effectively casting an A* to a B*. Since they're unrelated types, who knows what the operations on B do to an A object.
So.. you should cast that void* to a C* first, then assign the C* to a B* (no cast required). The compiler will know at that point that you're upcasting and will adjust the pointer value accordingly.
</Implementation-specific behaviour>
Cfr above: i don't know that i should go to a C until i got an A first,
because the A class contains some class specific definitions.
Now do i understand it right that the problem comes from the fact that
i'm using a void* ?
Would this be legal then:
// quick recap: class C is derived from both class A and B
A *ap;
B *bp;
C c,*cp;
cp=&c;
ap=cp;
bp=(B*)ap;
or the last line being
bp=static_cast< B*>(ap);
which is the same, right?
(i'm not laisy, i googled about it, but it's still not 100% clear)
John Harrison wrote:
Jo wrote:
>class A {
public:
char text_a[100];
A() { *text_a=0; } ~A() {} }; //-----------------------------------------------------------------------------
class B {
public:
char text_b[100];
B() { *text_b=0; } ~B() {} }; //-----------------------------------------------------------------------------
class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; } ~C() {} }; //-------------------------------------------------------------------------------
void test() {
B *bp1,*bp2; C c,*cp1,*cp2,*cp 3; void *p;
strcpy(c.text_a ,"hello a"); strcpy(c.text_b ,"hello b"); strcpy(c.text_c ,"hello c"); cp1=&c; p=cp1; bp1=cp1; // ok bp2=(B*)p; // resulting bp2 is WRONG! cp2=(C*)p; // ok cp3=(C*)bp2; // resulting cp3 is WRONG! Which is logical because bp2 is already wrong. } //-----------------------------------------------------------------------------
So the hot spot is the bp2=(B*)p;
What's wrong with that???
I can imagine someone saying "p is not pointing to a B object".
Right.
>But if you think about it, conceptually, it does, imho.
No, it doesn't.
>> So is it more a technical matter of compiling this!?
No the code is wrong.
>> Maybe i'm stupid and/or missing something essential about C++.
I doubt you're stupid but you're certainly missing something essential.
>> If so, please give me a link to where i can study this right.
I have no idea why you think that code should be right, that is my
problem.
Imho, if class C is derived from A and B, then C IS an A and a B at the
same time, conceptually.
But i'm understanding that there is a technical implementation problem here.
I thought the compiler could figure that out, but i'm clearly over
estimating the compiler and/or C++.
Anyway, it's off topic to go deeper in this. Although that would be a
nice discussion.
So i'll focus on the practical problem here.
Perhaps you are expecting an ordinary cast to work like a
dynamic_cast? When you write
bp2=(B*)p; // resulting bp2 is WRONG!
you are expecting the program to 'discover' that there a B object
hidden inside the object that p is pointing to?
If so google for dynamic_cast, or better still read a good book.
I understand that a dynamic_cast cannot do a base-to-derived conversion,
so that's not the solution here.
And a static_cast doesn't seem to make any difference, because i'm
already doing static casts right, just in the 'old' way.
So i'm still looking for the solution for this situation
class A {
public:
virtual long GetClassID() { return(' A'); }
};
class B {
public:
// some class data & funx
};
class C : public class A, public class B {
public:
virtual long GetClassID() { return(' C'); }
};
foo1(C *cp) {
...
foo2(cp);
...
}
foo2(A *ap) {
if (ap->GetClassID== ' C') {
C *cp=(C*)ap; // IS THIS LEGAL and PORTABLE C++ ?
}
}
class A {
public:
char text_a[100];
A() { *text_a=0; }
~A() {}
};
//-----------------------------------------------------------------------------
class B {
public:
char text_b[100];
B() { *text_b=0; }
~B() {}
};
//-----------------------------------------------------------------------------
class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; }
~C() {}
};
//-----------------------------------------------------------------------------
In the context of pointer casting: Does it matter for ansi C++ if class
C is derived from A then B, or from B then A ?
>If so google for dynamic_cast, or better still read a good book.
I understand that a dynamic_cast cannot do a base-to-derived conversion,
so that's not the solution here.
dynamic_cast does do base to derived conversion, that's it's main use.
Where are you getting your information?
john
//-----------------------------------------------------------------------------
>
class C : public A, public B {
public:
char text_c[100];
C() { *text_c=0; }
~C() {}
};
//-----------------------------------------------------------------------------
In the context of pointer casting: Does it matter for ansi C++ if class
C is derived from A then B, or from B then A ?
No it doesn't matter.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mike420 |
last post by:
In the context of LATEX, some Pythonista asked what the big
successes of Lisp were. I think there were at least three *big*
successes.
a. orbitz.com web site uses Lisp for algorithms, etc.
b. Yahoo store was originally written in Lisp.
c. Emacs
The issues with these will probably come up, so I might as well
mention them myself (which will also make this a more balanced
|
by: mike420 |
last post by:
Tayss wrote:
>
> app = wxPySimpleApp()
> frame = MainWindow(None, -1, "A window")
> frame.Show(True)
> app.MainLoop()
>
Why do you need a macro for that? Why don't you just write
|
by: AFN |
last post by:
I am running the code below to generate XML from a data table. But some
fields in the data table are Null for every record. Suppose field5 has a
null database value. I would expect to see:
<field5></field5> or <field5 />
but instead it doesn't even show the field at all for those records where
field5 is Null! Instead it just shows:
<field4>Whatever</field4>
|
by: Douglas Buchanan |
last post by:
Buttons don't work if form is opened on startup
A2k
If 'frmMain' is set to open by default at startup none of the buttons
work.
If 'frmMain' is opened from the database window then all the buttons
work.
The form's name ('frmMain') is selected from in the Startup dialog
box.
|
by: DFS |
last post by:
I'm unfortunately about to change, or sever, a two-year relationship with a
very slow-paying client.
Most of the 15 invoices I've sent them arrive 6 to 8 weeks late, and usually
only after repeated calls to the requesting client department, AP and
finally Legal. I really think they would never pay some of these invoices
if I didn't hound them. I don't have the time or energy or stress-handling
to make repeated collection calls and...
| |
by: Nashat Wanly |
last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaskdr/html/askgui06032003.asp
Don't Lock Type Objects!
Why Lock(typeof(ClassName)) or SyncLock GetType(ClassName) Is Bad
Rico Mariani, performance architect for the Microsoft® .NET runtime
and longtime Microsoft developer, mentioned to Dr. GUI in an e-mail
conversation recently that a fairly common practice (and one that's,
unfortunately, described in some of our...
|
by: LP |
last post by:
I am using (trying to) CR version XI, cascading parameters feature works it
asks user to enter params. But if page is resubmitted. It prompts for params
again. I did set ReuseParameterValuesOnRefresh="True" in a viewer, but it
still doesn't work. Did anyone run into this problem. What's the solution?
Please help.
Thank you
|
by: Frank |
last post by:
I've done this a few times.
In a solution I have a project, Say P1, and need another project that will
contain much code that is similar to that of P1.
I hope no one gets hung up on why I don't somehow share the code.
So, I copy the folder P1 is in, change the new folder name, and is VS2005 to
change all occurrences of P1's name tp P2's name.
|
by: jehugaleahsa |
last post by:
Hello:
I had an hour-long discussion with my boss today. Last night, right
before I dozed off, I realized some of his code resulted in duplicate
processing. I tried to explain it to him and he kept saying, "I'm
afraid to say it's 'OK' to change it because everything I did was for
a reason." Well, I know better.
My boss' old code is written in some cryptic C file with Oracle
precompiler macros barfed all over it. Once I figured out how...
|
by: Nepomuk |
last post by:
You might have heard about Linux, but you don't know what it is? Or you know a few things about it, but they terrify you? Well, then this article is for you. Don't be afraid - Everyone can use Linux!
Part 1: What is Linux and why should I use it?
Linux is an Operating System (short: OS). OK, so what is an OS? Let me explain:
Nearly everybody knows Windows. But what exactly does it actually do?
Windows is an example for a OS and one of it's...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |