2

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();
};
Quest
  • 2,764
  • 1
  • 22
  • 44
  • 1
    Please specify what actual issues do you experience with the code above (or some other code, in which case please provide it as well). – r3mus n0x Dec 23 '18 at 21:27
  • If you want to access `in_foo` outside of `foo` you should put the struct declaration after public. – doug Dec 23 '18 at 21:30
  • The provided code seems correct (except probably that `in_foo` should be `public` if one wants to use it after it called `foo()`). Please describe what problem your experiencing. – jpo38 Dec 23 '18 at 21:30
  • You can surely forward declare `in_foo` the way you did and define it in your cpp file. You might want to move it in public. But in general, this seems weird, because the user typically should know the definition of `in_foo` in order to use the return value. – Georgi Gerganov Dec 23 '18 at 21:31
  • @GeorgiGerganov Yeah, sorry. It's for internal use - it is actually a private method. I have no idea why i wrote it here as public. – Quest Dec 23 '18 at 22:42

2 Answers2

3

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).

jrok
  • 54,456
  • 9
  • 109
  • 141
  • 4
    I was just wondering why `auto` is allowed. Doesn't this violate the privateness of `in_foo`? Edit: found [discussion](https://stackoverflow.com/questions/13532784/why-can-i-use-auto-on-a-private-type) – Georgi Gerganov Dec 23 '18 at 21:38
  • @GeorgiGerganov yeah, this can be a bit unintuitive. – jrok Dec 23 '18 at 21:44
  • If you declare a member inside ```in_foo```, say a ```int x```, can you access to it via ```p->x```? (Assuming, e.g., that ```foo::func()``` returns a pointer to an existing ```in_foo``` object) – francesco Dec 23 '18 at 21:47
  • 1
    @francesco Yes, you can. – jrok Dec 23 '18 at 21:50
  • @francesco Here is [example](https://coliru.stacked-crooked.com/a/61eff9ada4c1d99e) – Georgi Gerganov Dec 23 '18 at 21:51
  • Thanks! I find it quite unintuitive. I understand that using ```auto``` you do not violate privateness of ```foo::in_foo``` because you do not *reveal* ```foo::in_foo```, but then I am nonetheless allowed to access its members. – francesco Dec 23 '18 at 21:56
  • Oh.. Now I see where the error is coming from.. I got `struct foo * in_foo;` I though it will declare a struct foo, and a variable in_foo, but I have to write it in a separate lines. Otherwise I get `no tag member named in_foo` error – Quest Dec 23 '18 at 22:37
0

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

jpo38
  • 20,821
  • 10
  • 70
  • 151