How is it correctly done? It needs to be declared bforehand in order to use it as a return value of a certain function within a class. Is it even possible?
For example:
class foo
{
struct in_foo;
public:
in_foo * func();
};
How is it correctly done? It needs to be declared bforehand in order to use it as a return value of a certain function within a class. Is it even possible?
For example:
class foo
{
struct in_foo;
public:
in_foo * func();
};
Yes, you can do it. Here's an example:
class foo
{
struct in_foo;
public:
in_foo * func();
};
struct foo::in_foo {
};
foo::in_foo* foo::func()
{
return nullptr;
}
int main()
{
foo f;
auto p = f.func(); // ok
// the following would be an error:
// foo::in_foo* p2 = f.func();
}
Note that, as shown above, you can only declare variables of foo:in_foo
by using auto
(as foo:in_foo
is private).
It's hard to understand what the question is, but the answer is probably "yes you can"...
foo.h file:
class foo
{
public:
struct in_foo;
in_foo * func();
};
foo.cpp file:
#include <iostream>
#include"foo.h"
struct foo::in_foo
{
int i;
};
foo::in_foo* foo::func()
{
foo::in_foo* res = new foo::in_foo();
res->i = 23;
return res;
}
int main()
{
foo::in_foo* temp = foo().func();
std::cout << temp->i;
delete temp;
}
This code will output 23
.
Note that struct in_foo;
must be after the public
statement, else, main can't declare foo::in_foo* temp