const reference in ctor.. why?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

const reference in ctor.. why?

Post by REDDemon »

Can anyone explain me why compiler (mingw) gives me errors if in the constructor I don't set a const reference?

wrong code

Code: Select all

 
//constructor
SomeClass::SomeClass(something & ref, foo * p) //compiler gives error but on 2nd parameter
{
 
}
 
returned error when I try to call constructor

Code: Select all

 
No matching function for call SomeClass::SomeClass(something & ref, foo *& p)
Candidates are...
 
No matching function for call SomeClass::SomeClass(something & ref, int)  //in this case null pointer is detected as "int" O_O
Candidates are...
 
correct code:

Code: Select all

 
//constructor
SomeClass::SomeClass(const something & ref, foo * p) //compile without errors.
{
 
}
 
I feel like I'm missing some fundamental thing. Tried to google and read FAQS but didn't find anything
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: const reference in ctor.. why?

Post by Mel »

The error says that you are trying to use *&p as the second parameter. The compiler understands that it is a pointer to a reference, when you have to pass just a pointer. I can't tell really why using a const on the first parameter will help though, can you post actual code?
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: const reference in ctor.. why?

Post by REDDemon »

No special code needed for that.

following just don't compile unless I put a const reference in Foo's constructor

Code: Select all

 
class Bar;
 
class MyClass
{
    MyClass(){}
};
 
class Foo
{
public:
    Foo(MyClass & myFoo,Bar * bar)
    {
 
    }
};
 
class Bar
{
public:
 
    Bar()
    {
 
    }
};
 
 

Seems that is impossibile mixing references and pointers in the same contructor (unless the reference is const).
Anyway I found some readings that tells to avoid Non-const references in constructors (without explaining why :/)
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
blAaarg
Posts: 94
Joined: Tue Mar 02, 2010 9:11 pm
Location: SoCal

Re: const reference in ctor.. why?

Post by blAaarg »

Anyway I found some readings that tells to avoid Non-const references in constructors (without explaining why :/)
Hmm, I don't know about mingw specifically, but perhaps they are saying that--"conceptually" speaking--constructors should just take external data for copying and not alter it.

I hadn't read that before but I suppose it makes sense, unless you're using a manipulator or function object or something. (I don't know if you would even want a functor to do that. It seems messy. I just meant that there may be exceptions to whatever that rule is.)
"Computers don't make mistakes! What they do they do on purpose!!"

-Dale Gribble
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: const reference in ctor.. why?

Post by CuteAlien »

Your code above compiles here on MinGW (once I add a main()). And using non-const references in constructors should be all right - if you have references in your class this is even the only way to set them.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: const reference in ctor.. why?

Post by REDDemon »

CuteAlien wrote:Your code above compiles here on MinGW (once I add a main()). And using non-const references in constructors should be all right - if you have references in your class this is even the only way to set them.

that compile ?O_O i'll try to re-install. still don't compile here O_O thx for trying
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: const reference in ctor.. why?

Post by serengeor »

Compiles for me too on mingw.
Working on game: Marrbles (Currently stopped).
Post Reply