Reputation: 393
I have this weird problem where the values are well-put in the constructor, but this one doesn't return the right object, it just returns an object with wrongly set values (the default one, I suppose)
Take a look at this screen:
I am sure that the data is right when I debug and enter into the constructor. Here is for example the constructor of UserDetails:
UserDetails::UserDetails( QString _maxDataRate, Request _request ){
unsigned int intMaxDataRate = _maxDataRate.toUInt();
UserDetails(intMaxDataRate, _request);
}
UserDetails::UserDetails( unsigned int _maxDataRate, Request _request ) :
maxDataRate( _maxDataRate ), request( _request ) {
this->userID = IDGenerator++;
}
I know this must be a classic C++ error, like a reference problem or something like that, but I don't remember what this could be.
Upvotes: 1
Views: 512
Reputation: 16324
You are calling the constructor from within the constructor, i.e. you are creating a temporary object which is destroyed immediately.
You need to do the following:
UserDetails::UserDetails( QString _maxDataRate, Request _request )
: UserDetails(_maxDataRate.toUInt(), _request);
{
}
Upvotes: 3
Reputation: 66371
You can't call a constructor from another - the line
UserDetails(intMaxDataRate, _request);
creates a different, unnamed, instance of UserDetails
which is destroyed immediately.
In C++11, you can forward the constructor to another:
UserDetails::UserDetails(QString _maxDataRate, Request _request)
: UserDetails(_maxDataRate.toUInt(), _request);
{
}
in C++03 and earlier, you'll need to either duplicate the constructor code or use a separate function that you call from both.
Side note: in this case, I believe that you shouldn't have a constructor that accepts a string at all, but perform the conversion (and validate it) outside the class.
Upvotes: 1