Hello ladies and gents,
I was reading about how "Passing by References" for efficiency and there was a little program added wich shows what the difference is between passing by value and passing by reference is. The program is this one:
#include <iostream>
using namespace std;
class SimpleCat
{
public:
SimpleCat();
SimpleCat (SimpleCat&);
~SimpleCat();
};
SimpleCat::SimpleCat()
{
cout<<"Simple Cat Constructor...\n";
}
SimpleCat::SimpleCat (SimpleCat&)
{
cout<<"Simple Cat Copy Constructor...\n";
}
SimpleCat::~SimpleCat()
{
cout<<"Simple Cat Destructor...\n";
}
SimpleCat FunctionOne (SimpleCat theCat);
SimpleCat *FunctionTwo (SimpleCat *theCat);
int main()
{
cout<<"Making a cat...\n";
SimpleCat Frisky;
cout<<"Calling FunctionOne...\n";
FunctionOne (Frisky);
cout<<"Calling FunctionTwo...\n";
FunctionTwo (&Frisky);
return 0;
}
SimpleCat FunctionOne (SimpleCat theCat)
{
cout<<"FunctionOne returning...\n";
return theCat;
}
SimpleCat *FunctionTwo (SimpleCat *theCat)
{
cout<<"FunctionTwo returning...\n";
return theCat;
}
No problem there, but, I decided to see wether I could change the program so "FunctionOne" and "FunctionTwo" where memberfunctions from SimpleCat, but, to no avail.
I thought that the following changes would be correct and lett me use this in the same way as the above example, but, that is not the case:
#include <iostream>
using namespace std;
class SimpleCat
{
public:
SimpleCat();
SimpleCat (SimpleCat&);
~SimpleCat();
FunctionOne (SimpleCat);
*FunctionTwo (SimpleCat*);
};
SimpleCat::SimpleCat()
{
cout<<"Simple Cat Constructor...\n";
}
SimpleCat::SimpleCat (SimpleCat&)
{
cout<<"Simple Cat Copy Constructor...\n";
}
SimpleCat::~SimpleCat()
{
cout<<"Simple Cat Destructor...\n";
}
int main()
{
cout<<"Making a cat...\n";
SimpleCat Frisky;
cout<<"Calling FunctionOne...\n";
Frisky.FunctionOne (Frisky);
cout<<"Calling FunctionTwo...\n";
Frisky.FunctionTwo (&Frisky);
return 0;
}
SimpleCat::FunctionOne( SimpleCat theCat)
{
cout<<"FunctionOne returning...\n";
return theCat;
}
SimpleCat::*FunctionTwo (SimpleCat *theCat)
{
cout<<"FunctionTwo returning...\n";
return theCat;
}
The error messages that I get are these ones:
error C2501: 'FunctionTwo' : missing storage-class or type specifiers
error C2440: 'return' : cannot convert from 'class SimpleCat' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be callederror C2501: 'FunctionTwo' : missing storage-class or type specifierserror C2440: 'return' : cannot convert from 'class SimpleCat *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2617: 'FunctionTwo' : inconsistent return statement
Can any of you please explain what I'm doing wrong :?:
Thank you.