Accessing an instance of a class defined in main in another class - c++

The problem i've been facing is related to accessing an instance of a class, I'll explain it through a series of code snipets:
If I have a class Foo defined as defined below:
class Foo {
public:
Foo(){x=5}
private:
int x;
}
And I create an instance of that object in main as follows:
int main(){
Foo a;
}
I then want to access that instance of that object in another class and store it:
class Bar{
public:
Bar() {copy = a}
private:
Foo copy;
}
How would that be possible? Is there a way around this? Any help is appreciated!

The most natural way would be to pass the Foo object as an argument to the Bar constructor:
Bar(Foo a)
{
std::cout << a.x << '\n';
}
For the updated question, I might pass the object as a constant reference instead of by value:
Bar(Foo const& a)
: copy{ a }
{
}
This will initialize the member variable copy to be a copy of the Foo object that a references.
For the Qt special case, you should almost never make copies of QObject-derived objects. Instead you should have pointers to them:
class Bar
{
public:
Bar(Foo* a)
: pointer{ a }
{
}
private:
Foo* pointer;
};

Related

Accessing private members of surrounding class

I have two classes like this:
#include <iostream>
class A {
public:
class B {
public:
void printX(void) const { std::cout << A::x << std::endl; }
};
private:
int x;
};
Obviously this piece of code doesn't work because B can't access x, but is there a way to make it work?
I've tried using friend class keyword in both classes like this:
class A {
public:
class B {
public:
friend class A;
void printX(void) const { std::cout << A::x << std::endl; }
};
friend class B;
private:
int x;
};
But it didn't work either, and I can't figure out if it's even possible.
According to the C++ 17 Standard (14.7 Nested classes)
1 A nested class is a member and as such has the same access rights as
any other member. The members of an enclosing class have no special
access to members of a nested class; the usual access rules (Clause
14) shall be obeyed.
The problem with the provided code is that x is not a static data member of the class A. You need to provide an object of the class A the data member x of which will be accessed within an object of the nested class.
For example
class A {
public:
class B {
public:
void printX( const A &a ) const { std::cout << a.x << std::endl; }
};
private:
int x;
};
A::B().printX( A() );
As the error message should tell you, A::x isn’t a static member so you need an object instance to access it. If you add a reference to instance of A to B::A, you can use that to access A::x.
For example, the following works:
class A {
public:
class B {
public:
B(A const& a) : a(a) {}
void printX(void) const { std::cout << a.x << std::endl; }
private:
A const& a;
};
private:
int x;
};
Note that using a reference member has several implications. Notably, you can now no longer reassign instances of type A::B, nor are its instances movable. As a consequence, it’s often convenient to hold a pointer rather than a reference to A inside A::B. Either way, you’ll need to ensure that instances of A::B do not outlive the A instance they refer to, otherwise you end up with a dangling reference.

c++ Instance of class in same class, using constructor

I have class A, is it possible to make sth like this? A(A(A()))? I mean creating some object in constructor and so on?
class A {
private:
A *a;
public:
A(){
printf("A()\n");
}
A( A * var){
printf("A(A)\n");
a = var;
}
};
int main() {
A x = A(A(A()));
return 0;
};
and this give me output A(), i mean why not A() A(A) A(A)? My A class represent a memory cell so A(1) is pointing to array[1] but A(A(1)) is sth like array[array[1]]. But i don't have idea how to implement this, only one object is created. (this A class is just an example of what I want to achieve)

C++: "Upgrading" pointer type of subclass member

Let's say I have the following class hierarchy:
class A {
int x;
};
class B : public A {
float y;
};
class Foo {
A* obj;
};
class Bar : public Foo {
B* obj; // I want this to override "obj" in the superclass!
};
I'm aware that Bar will shadow the obj of the superclass Foo, and that actually Bar will have two fields. But what I really want is that, if some code is able to see an object of type Bar*, they may also assume its pointer obj is referring to a type B. How can I accomplish this?
I've thought of two ways:
Add a template to Foo, which specifies the type of its pointer. Then, have Bar override as Bar : public Foo<B>. This adds templates and may be ugly.
Make the members protected, add getters in Bar which cast obj to B*, and make sure (in constructors, setters, etc.) that obj is only ever set to a B* type. This is inelegant because it's not really compile-time and requires a cast.
Are there better solutions available, that don't have the disadvantages of the above?
Thanks!
Try this thing:
class A {
int x;
};
class B : public A {
float y;
};
class Foo {
protected:
A* obj;
};
class Bar : public Foo {
B* obj; // I want this to override "obj" in the superclass!
public:
void edit_foo_obj(A* n_obj) {
Foo::obj = n_obj;
}
void edit_bar_obj(B* n_obj) {
this->obj = n_obj;
}
};
int main()
{
Bar b;
A a;
B ba;
b.edit_bar_obj(&ba);
b.edit_foo_obj(&a);
return 0;
}

Initialization of base class value for derived class

I want to create ab object of the base class Foo that initializes the variable value to something I decide, I then want to create an object of the derived class Bar and the variable value I decided earlier sticks with Foo that Bar inherits, like a constant one time initialization for the variable value.
Is there a way to do this without passing in the value every time I create the Bar object like with the Foo object or make the class value default to 5?
Example:
// Base class
class Foo {
private:
int value;
public:
Foo() {}
Foo(int value) : value(value) {}
int getValue() { return value; }
};
// Derived class
class Bar : public Foo {
private:
public:
Bar() {}
};
Foo foo(5);
foo.getValue() // 5
Bar bar;
bar.getValue() // this I also want to be 5 now
You can do this with static variables, either in class scope or method scope (with the latter having some differences, like lazy initialization, thread safe initialization, etc. Although, it does not make a big difference in your use case, but it is good to keep the latter in mind as an option). For example
#include <iostream>
using std::cout;
using std::endl;
// Base class
class Foo {
private:
static int value;
public:
Foo() {}
Foo(int value) {
Foo::value = value;
}
int getValue() { return value; }
};
int Foo::value = 0;
// Derived class
class Bar : public Foo {
private:
public:
Bar() {}
};
int main() {
Foo foo(5);
cout << foo.getValue() << endl;
Bar bar;
cout << bar.getValue() << endl;
}
I have just provided you with a solution you want. Keep in mind that this might not be the best way to achieve what you want. An object's construction parameters should ideally only affect the current object.

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.