C++: Order of declaration in public and private - c++

I am unsure about the order of declaration in my class. My compiler says error: "Foo" was not declared in this scope. If I change the order of public and private parts of the class, I end up with the same error. Also, if I want to use the getFoo(), and I am including my header file, the struct Foo type is not visible because it's private. But if I put it in public scope again, then public would have to come before private because otherwise the declaration of myFoo of type Foo can't happen since Foo was not decalred yet.
I am confused here... thanks for your help!
/*MyClass.h*/
class MyClass
{
private:
struct Foo{
int bar;
};
Foo myFoo;
public:
Foo getFoo(){ return myFoo;}
};

It has nothing to do with public or private. Your inner type has to be defined before it is used:
struct Foo
{
Bar bar() { return Bar(); } // ERROR
struct Bar {};
};
struct Foo
{
struct Bar {};
Bar bar() { return Bar(); } // OK
};
Note: there has been some confusion concerning the accessibility of a private type. THe type is accessible outside of its class, it just cannot be named. So, this code that accesses the private type is completely legal:
class Foo
{
struct Bar {
void do_stuff() const {}
};
public:
Bar bar() { return Bar(); } // OK
};
int main()
{
Foo f;
f.bar().do_stuff(); // use temporary Bar object
// In C++11, you can even make a named instance
auto b = f.bar(); // instantiate a Bar object called b
b.do_stuff(); // use it
}

Your struct Foo should be public otherwise getFoo getter will not work as Foo is accessible only inside your class

Related

Only let the specific class 'Fabric' construct instances of class 'Foo' and all of its subclasses

Is there a way to ensure that only class Fabric can construct the class Foo and all of its sub-classes, without having to declare a private constructor and friend class Fabric in each sub-class?
struct Foo {
friend class Fabric;
protected:
Foo() = default;
};
struct FooConcrete1 : Foo {
friend class Fabric;
protected:
Foo() = default;
};
Since friendship is not inherited, both manual actions when declaring each sub-class seem to be needed, which is error-prone.
One option is to declare a tag structure which is only constructible by Fabric and pass this object to the constructor of Foo. If you forget to add the constructor to derived classes you will get an error that Foo isn't default constructible.
struct FooTag
{
friend struct Fabric;
private:
FooTag();
};
struct Foo {
Foo(FooTag tag) {}
};
struct FooConcrete1 : Foo {
using Foo::Foo;
};
struct Fabric
{
void test()
{
FooConcrete1 f = FooConcrete1(FooTag());
}
};
int main()
{
FooConcrete1 f; // can't construct as we can't construct FooTag
return 0;
}

How to construct member class object from outside class (c++)

I have something that looks like this:
class Foo
{
public:
Foo(parameter);
}
class Bar
{
Foo fooObject;
}
Bar::fooObject(data);
But I get:
error: expected constructor, destructor, or type conversion before '(' token
So my question is: how do I construct fooObject from outside Bar?
You can have a constructor that take an instance of Foo from outside of class and then you can copy or move that instance to your member variable. Another approach would be to take required parameters to construct an instance of Foo and then use member initializer list to construct member variable.
class Bar
{
public:
Bar(const Foo& foo) // Copy
: fooObject(foo)
{
}
Bar(Foo&& foo) // Move
: fooObject(std::move(foo))
{
}
Bar(int example)
: fooObject(example)
{
}
private:
Foo fooObject;
}
Initialize members using a constructor and member initializer list:
class Foo{
public:
Foo(int x);
};
class Bar{
public:
Bar(int param);
private:
Foo fooObject;
};
Bar::Bar(int param) : fooObject(param){};
If by outside of the class you mean the class member function definition then you would use the following syntax that goes into a source (.cpp) file:
Bar::Bar(int param) : fooObject(param){}; // ctor definition outside the class
The declarations can be placed in a header file.

Grant access to private constructor without friends?

I am working on some code, where I encountered a situation similar to this one:
struct Bar;
struct Foo{
friend struct Bar;
private:
Foo(){}
void f(){}
void g(){}
};
struct Bar {
Foo* f;
Bar() { f = new Foo();}
~Bar() { delete f;}
};
int main(){
Bar b;
}
I would prefer to have Bar not as friend of Foo, because besides Foos constructor Bar does not need access to any of Foos private methods (and thus should not have access). Is there a way to allow only Bar to create Foos without making them friends?
PS: realized that the question might not be 100% clear. I don't mind if it is via friends or not, just the fact that all Bar has access to all private methods is disturbing me (which is usually the case with friends) and that is what I want to avoid. Fortunately none of the answers given so far had a problem with that lousy formulation.
This is precisely what the attorney-client idiom is for:
struct Bar;
struct Foo {
friend struct FooAttorney;
private:
Foo(){}
void f(){}
void g(){}
};
class FooAttorney {
static Foo* makeFoo() { return new Foo; }
friend struct Bar;
};
struct Bar {
Foo* f;
Bar() { f = FooAttorney::makeFoo();}
~Bar() { delete f;}
};
int main(){
Bar b;
}
In a code imitates life fashion, the class declares an attorney that will mediate the secrets it's willing to share with the selected parties.
If you do not want to introduce another class, you can shrink the circle of friendship and make Bar's constructor Foo's friend. It requires Bar's definition to be available to Foo, and it still gives Bar's constructor unrestricted access to Foo's private implementation:
struct Foo;
struct Bar {
Foo* f;
Bar();
~Bar();
};
struct Foo{
friend Bar::Bar();
private:
Foo(){}
void f(){}
void g(){}
};
Bar::Bar() : f(new Foo()) {
}
Bar::~Bar() {
delete f;
}
This does not achieve exactly what you want, but it makes friendship a lot more targeted.
One way that occurred to me was to have an internal class that makes Bar its friend so only Bar can create it and that internal class can be used as an additional parameter to Foo constructor so only the class's friends can invoke it.
class Foo
{
public:
// only friends of the special key can invoke the constructor
// or any member function that includes it as a dummy parameter
class special_key {friend class Bar; special_key(){}};
// making special_key a dummy parameter makes sure only friends of
// the special key can invoke the function
Foo(special_key) {}
void f(){}
void g(){}
};
class Bar
{
public:
// only Bar functions can create the special key
Bar() { f = std::make_unique<Foo>(Foo::special_key()); }
private:
std::unique_ptr<Foo> f;
};
As well as restricting access to specific functions this technique also allows the use of smart pointer make functions which direct friendship does not.

Accessing nested class constructor

How can I write code for a nested class' constructor? Because the following example gives me errors
foo.h
class foo
{
public:
class bar
{
public:
bar();
~bar();
}
private:
}
foo.cpp
#include "foo.h"
foo::bar()
{
}
You have to explicitly name the constructor as a member of foo::bar, not of foo. Adjust the name like this:
foo::bar::bar() {}
// ^^^^^
Since bar is a "derived class" of foo, which means if you want to access function bar(), you need to access the class bar first.
bar() is in the namespace of foo::bar::
You need to try foo::bar::bar()
class foo {
public:
foo() : _bar(this) {}
class bar {
public:
bar(const foo* base);
~bar() {}
private:
const foo* _base;
};
private:
bar _bar;
};
foo::bar::bar(const foo* base) : _base(base) {}
add in a variable in there to also show how you can call the outer class functions inside the nested class if need as well.
As a side not you also have error with your syntax. You are missing a ; on the classes closing }.

How to limit access to class function from main function?

How to limit access to class function from main function?
Here my code.
class Bar
{
public: void doSomething(){}
};
class Foo
{
public: Bar bar;
//Only this scope that bar object was declared(In this case only Foo class)
//Can access doSomething() by bar object.
};
int main()
{
Foo foo;
foo.bar.doSomething(); //doSomething() should be limited(can't access)
return 0;
}
PS.Sorry for my poor English.
Edit:
I didn't delete old code but I expand with new code.
I think this case can't use friend class. Because I plan to use for every class. Thanks
class Bar
{
public:
void A() {} //Can access in scope that object of Bar was declared only
void B() {}
void C() {}
};
class Foo
{
public:
Bar bar;
//Only this scope that bar object was declared(In this case is a Foo class)
//Foo class can access A function by bar object
//main function need to access bar object with B, C function
//but main function don't need to access A function
void CallA()
{
bar.A(); //Correct
}
};
int main()
{
Foo foo;
foo.bar.A(); //Incorrect: A function should be limited(can't access)
foo.bar.B(); //Correct
foo.bar.C(); //Correct
foo.CallA(); //Correct
return 0;
}
Make Foo a friend of Bar
class Bar
{
friend class Foo;
private:
void doSomething(){}
};
And also avoid making member variables public. Use setters/getters instead
You can define Foo as a friend class of Bar and make doSomething() private.
Making Bar bar private inside Foo would do the trick, would it not?
Then only the class Foo could use bar.