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

Const reference problem

100 100+
Hi
I have a class A
Defind like this
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {public:
  3. const ClassB &bInstance;
  4. void function();
  5. }
  6.  
Class ClassB is defined like this
Expand|Select|Wrap|Line Numbers
  1. class ClassB
  2. {
  3. public:
  4. void publish();
  5. }
  6.  


If i want to use the bInstance....How can i use....
i tried like this....
Expand|Select|Wrap|Line Numbers
  1. void A::function()
  2. {
  3.     bInstance.publish();
  4. }
  5.  
  6.  
I gave me an error "cannot convert cont class to class &"

Soooo I changed the code like this...
Expand|Select|Wrap|Line Numbers
  1. void A::function()
  2. {
  3.    ClassB *temp=(ClassB*)&bInstance;
  4.     temp->publish();
  5. }
  6.  
  7.  
  8.  

And it worked...But i want to know the exact reason....please someone help....
Jul 26 '07 #1
2 1354
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {public:
  3. const ClassB &bInstance;
  4. void function();
  5. }
  6.  
Expand|Select|Wrap|Line Numbers
  1. class ClassB
  2. {
  3. public:
  4. void publish();
  5. }
  6.  
You have declared the member of type B (bInstance) in A as a constant reference. That means that A can not change the data referenced.

However in class B the function publish is declared normally, that means that it may change some of the member data of B so when you declare

Expand|Select|Wrap|Line Numbers
  1. void A::function()
  2. {
  3.     bInstance.publish();
  4. }
  5.  
You are using a constant reference to try and call a function that may change the instance of B, that is not allowed.

All the casts you did to make it work are a very bad idea, you have basically hacked your way round the the C++ type checking which, when left alone, greatly increase the robustness of your program.

You should do 1 of 2 things either

1. Change the declaration of B
Expand|Select|Wrap|Line Numbers
  1. class ClassB
  2. {
  3. public:
  4.    void publish() const;
  5. }
  6.  
Adding the const keyword indicates that the function publish does not change any members of B.

2. Change the declaration of A
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     ClassB &bInstance;
  5.     void function();
  6. }
  7.  
Removing the const keyword indicates that A can use the reference to change the instence of B referenced.
Jul 26 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
void A::function()
{
ClassB *temp=(ClassB*)&bInstance;
temp->publish();
}
Do not cast. bInstance is a const reference. To use it, your function must be const:
Expand|Select|Wrap|Line Numbers
  1. void A::function() const
  2. {
  3.   bInstance.publish();
  4. }
  5.  
That will re-assure the compiler that A::function() is not going to change member data.

Casting in C++ usually means a) you are calling a relic C funciton that has void* arguments or some such, or b) your C++ design is weak.

That cast requires the compiler to create a non-const copy of bInstance. That requires a ClassB copy constructor call plus potentially copy constructor call for other members of ClassB plus potentially copy constructor calls for memebers of ClassB that are also class objects, etc... into the sunset.
Jul 26 '07 #3

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

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.