Hello everyone,as you saw in the topic a was wondering if i could create a function which returns a class object.I writed the following code but obviously it returns me erros.Take a look:
#include<iostream>
usingnamespace std;
class foo()
{
class one
{
public:
int k;
one(){k=6;}
private:
};
one tsiou;
return tsiou;
}
int main()
{
foo();
return 0;
}
The compiler tells me that foo is a class "'return' : cannot convert from 'class foo::one' to 'int'"
I understant that the object is declared in a topic area so i tried to made it accesable by the return method.If this is totally wrong are there any other sugestions about how to return a class object from a function.Thanks for your time.
I've never seen a class with a return function. You should write a member function of the class foo that returns an object of the class one. And do not declare classes within classes!
You need to first define the class one, then have the function foo of return type one, then (optionally) when you call foo ensure its output is put to a instance of one.
At the moment you have a class foo containing a class one, I'm suprised the compilation doesn't stop earlier.
You need a structure kind of like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class one
{
public:
one() {};
/// ... class features
};
one foo()
{
one result;
return result;
}
int main()
{
one mine = foo();
return 0;
}
edit to add: @xander333 - nested classes are indeed awesome, if you know where to use them.
Pax nice and easy tip,i never thought i could do something like that!
Moorecm ouaou ,that's an realy andvance style,i will give it a try (i will search for the book also,i hope i can understant it,since i am a beginner yet.)