473,396 Members | 1,771 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,396 software developers and data experts.

passing a pointer to base class as reference initializer

On gnu compiler:

If you try to initialize a reference to a pointer to a base type, (e.g. Foo*&), with a pointer to a derived type, eg Bar*, the compiler rejects it claiming inability to match prototypes.

Is this a gnu compiler bug or is there sanity behind its recalcitrance?
Microsoft compiler is OK with this im told (but havent tried myself)
For example, casting to the base type pointer prior to function call does not help. But creating a separate base class pointer and initializing it by direct cast on the derived class will work. Go figure!!

See below:


struct Foo {
int a;
double b;
};

struct Bar : public Foo {
double c;
};

void afunc(Foo *& e) {
e->a = 23;
e = NULL;
}

void cfunc(Foo *& e) {
e->b = 34.5;
e = NULL;
}

void testfunc() {
Foo foo;
Foo *pfoo = &foo;
afunc(pfoo); // works fine


Bar bar;
Foo *pbar = &bar;
cfunc(pbar); // works fine, but requires explicit Foo* pointer

// Bar *ppbar = &bar;
// cfunc((Foo *)ppbar); // compiler don’t like this, even with explicit cast
// I would expect
// cfunc(ppbar); to be ok, and certainly,
// cfunc((Foo *)ppbar); to be ok given that 6 lines above is ok by compiler
// can someone explain what I am missing please.

}
Feb 15 '08 #1
4 1869
weaknessforcats
9,208 Expert Mod 8TB
Bar bar;
Foo *pbar = &bar;
cfunc(pbar); // works fine, but requires explicit Foo* pointer

// Bar *ppbar = &bar;
// cfunc((Foo *)ppbar); // compiler don’t like this, even with explicit cast
// I would expect
// cfunc(ppbar); to be ok, and certainly,
First, your naming convention has tripped you up. ppbar is not a pointer to a Bar*. It's a Bar*.
Second, cfunc() takes a reference to a pointer as an argument but you do not provide the pointer to use used a reference. You attempt to do this with a cast but that won't work. You need to create an actual Foo* to be used as a reference. See below, which compiles and links:

[
Expand|Select|Wrap|Line Numbers
  1. Bar *pbar1 = &bar;
  2. Foo* test = pbar1;
  3. cfunc(test); 
  4.  
Feb 15 '08 #2
First, your naming convention has tripped you up. ppbar is not a pointer to a Bar*. It's a Bar*.
Second, cfunc() takes a reference to a pointer as an argument but you do not provide the pointer to use used a reference. You attempt to do this with a cast but that won't work. You need to create an actual Foo* to be used as a reference. See below, which compiles and links:

[
Expand|Select|Wrap|Line Numbers
  1. Bar *pbar1 = &bar;
  2. Foo* test = pbar1;
  3. cfunc(test); 
  4.  

Thank you weaknessforcats for your response, and please excuse my novice posting etiquette.
Your resonse is correct, however it did not help me understand that which is puzzling me. A simpler example, that does not involve functions syntax is as follows:

Expand|Select|Wrap|Line Numbers
  1.   Bar bar;
  2.   Bar *pbar  = &bar;
  3.   Foo *pbar_ = &bar;
  4.  
  5.   Bar* &rpbar = pbar;
  6.   Foo* &rpbar1 = pbar_;
  7.   Foo* &rpbar2 = (Foo *)pbar;  // fails with compiler message as below
  8.  
error: invalid initialization of non-const reference of type 'const Foo*&' from a temporary of type 'const Foo*'

Above,
pbar_ is a pointer to Foo initialized with address of instance bar and can be used to initialize rpbar1, a reference to pointer to Foo

pbar is a pointer to Bar, initialized with address of instance bar, however, even after casting it explicitly to (Foo*) it cannot be used to initialize rpbar2, a reference to pointer to Foo.

To me,
(Foo *)pbar; on the right hand side of = on line 7) is the same as
pbar_; on the right hand side of = on line 6).

Both right hand sides are of type Foo*

1). I dont understand why they are different to the compiler
2). I dont understand the error message that is reported.
3). I would have expected a line 7) without the explicit cast to work because Bar is a kind of Foo.

Can you explain all this to me?
Feb 15 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
To me,
(Foo *)pbar; on the right hand side of = on line 7) is the same as
pbar_; on the right hand side of = on line 6).

Both right hand sides are of type Foo*
It's because you are initializing a reference.

To do that, you need an actual object to refer to. A typecast can produce a tempary object, but not always, and that temprary object does last as long as the current scope. It goes away at the end of the statement. That makes it not suitable for a reference.

Just initilize a variable with the result of the typecast and you can refer to that.
Expand|Select|Wrap|Line Numbers
  1. Bar* &rpbar = pbar;
  2.   Foo* &rpbar1 = pbar_;
  3.    Foo* x = (Foo*)pbar;
  4.    Foo*& rpbar2 = x;
  5.  
Feb 19 '08 #4
It's because you are initializing a reference.

To do that, you need an actual object to refer to. A typecast can produce a tempary object, but not always, and that temprary object does last as long as the current scope. It goes away at the end of the statement. That makes it not suitable for a reference.

Just initilize a variable with the result of the typecast and you can refer to that.
Expand|Select|Wrap|Line Numbers
  1. Bar* &rpbar = pbar;
  2.   Foo* &rpbar1 = pbar_;
  3.    Foo* x = (Foo*)pbar;
  4.    Foo*& rpbar2 = x;
  5.  

Thanks for your input.
I appreciate.
Best YW
Feb 19 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
3
by: Zheng Da | last post by:
Will the following class work well? If it can not work correctly in some situation, could you help me to fix it? Thank you. //the class will work like the reference in java //when you create a...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
1
by: qwerty2_reverse_iterator | last post by:
Is this a bug with the ms compiler (V7.1)? (It seems so at least.) I get errors when I don't initialize all the const pointer fields of an anonymous union in a struct. Example: //T2.h...
7
by: Johannes Bauer | last post by:
Hello Group, please consider the following code #include <vector> #include <iostream> #define USE_CONST #define USE_STRING
7
by: tech | last post by:
Hi, thanks for help in previous post. I've made the class objects i need data members so i now initialise in constructor. However some of my subobjects need to have a reference to their owner...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.