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.

const reference in ctor.. why?

Postby REDDemon » Sat Jun 23, 2012 8:41 am

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

wrong code
cpp 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
cpp 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:
cpp 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
OpenGL is not hard. What you have to do is just explained in specifications. What is hard is dealing with poor OpenGL implementations.
User avatar
REDDemon
 
Posts: 832
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: const reference in ctor.. why?

Postby Mel » Sat Jun 23, 2012 8:56 am

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?
http://santiagong.daportfolio.com/
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
User avatar
Mel
Competition winner
 
Posts: 1788
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: const reference in ctor.. why?

Postby REDDemon » Wed Jun 27, 2012 8:05 pm

No special code needed for that.

following just don't compile unless I put a const reference in Foo's constructor
cpp 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 :/)
OpenGL is not hard. What you have to do is just explained in specifications. What is hard is dealing with poor OpenGL implementations.
User avatar
REDDemon
 
Posts: 832
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: const reference in ctor.. why?

Postby blAaarg » Thu Jun 28, 2012 1:56 am

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
blAaarg
 
Posts: 94
Joined: Tue Mar 02, 2010 9:11 pm
Location: SoCal

Re: const reference in ctor.. why?

Postby CuteAlien » Thu Jun 28, 2012 8:32 am

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.freenode.net
My patches&stuff: http://www.michaelzeilfelder.de/irrlicht.htm
Games with Irrlicht: http://www.irrgheist.com/
News: http://www.reddit.com/r/irrlicht/
User avatar
CuteAlien
Admin
 
Posts: 5396
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany

Re: const reference in ctor.. why?

Postby REDDemon » Fri Jun 29, 2012 6:34 am

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
OpenGL is not hard. What you have to do is just explained in specifications. What is hard is dealing with poor OpenGL implementations.
User avatar
REDDemon
 
Posts: 832
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: const reference in ctor.. why?

Postby serengeor » Fri Jun 29, 2012 10:12 am

Compiles for me too on mingw.
Working on game: Marrbles (Currently stopped).
User avatar
serengeor
 
Posts: 1701
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania


Return to Beginners Help

Who is online

Users browsing this forum: No registered users and 1 guest