How to access private members of a nested class? - c++

A.hpp
class A
{
public:
class B
{
int x;
public:
B(int f);
};
void alpha(B *random);
};
A.cpp
void A::alpha(A::B *random)
{
// access x here, how to do it?
}
The private variable is getting set at some place and I want to access that value in this alpha function. How can I access x inside alpha()?
EDIT: 2nd Question:
A.hpp
class A
{
public:
class B
{
int x;
public:
B(int f);
};
virtual void alpha(B *random) = 0;
};
C.hpp
class C : public A
{
public:
virtual void alpha(B *random);
};
C.cpp
void C::alpha(A:B *random)
{
// access x here, how to do it?
}

You can make class A a "friend" of class B which allows A to access private members of B
combining your files for ease of compilation:
class A
{
public:
class B
{
friend class A; // *** HERE ***
int x;
public:
B(int f);
};
void alpha(B *random);
};
void A::alpha(B *random)
{
int x = random->x;
}
int main() {}

A class can access another class's private members if it's a friend of that other class, like in this example:
class A {
public:
class B {
friend class A;
int x;
public:
B(int f);
};
void alpha(B *random) { random->x = 10; }
};

Related

Learning Classes C++ - Can't set values in an array of classes nested within another class.

I am super-new to classes and still wrapping my brain around how they work. Any help/advice/pointers-> are appreciated!
I have two classes. Within the second class is an array of the first class. I am trying to assign values to the private member variables contained in the array of the first class.
I get this error message when compiling:
hw2Test.cpp: In member function 'void bar::set(int)':
hw2Test.cpp:11:7: error: 'int foo::x' is private
int x;
^
hw2Test.cpp:34:12: error: within this context
foodoo[0].x = x;
^
Here is the code:
#include <iostream>
using namespace std;
class foo
{
public:
private:
int x;
};
class bar
{
public:
void set(int x);
private:
foo foodoo[1];
};
int main()
{
bar tar;
tar.set(1);
return 0;
}
void bar::set(int x)
{
foodoo[0].x = x;
}
foo::x is declared as private, so only methods of foo can access it. But you are trying to access x inside of a method of bar instead, which does not have access to foo's private members.
To give bar access, you need to either:
declare foo::x as public:
class foo
{
public:
int x;
};
void bar::set(int x)
{
foodoo[0].x = x;
}
declare a public setter:
class foo
{
public:
void set(int i);
private:
int x;
};
void foo::set(int i)
{
foodoo[0].x = i;
}
void bar::set(int x)
{
foodoo[0].set(x);
}
declare bar as a friend of foo:
class foo
{
public:
private:
int x;
friend class bar;
};
void bar::set(int x)
{
foodoo[0].x = x;
}

C++ design pattern - member-only class

I want a class that can only be instantiated as a member of another class.
Id est:
class A
{
public:
A() :
member_()
{};
void letBSayHi() { member_.sayHi(); }
private:
B member_;
};
class B
{
public:
void sayHi() { printf("hola!"); }
};
thus:
A alpha; // valid
alpha.letBSayHi(); // # hola!
B beta; // invalid
beta.sayHi(); // impossible
The singleton pattern obviously wouldn't work, as I want one instance of class B for every instance of class A. But any instantiation of class B other than as a class A-member should be prohibited.
Make B a private nested class of A:
class A {
public:
void letBSayHi() { member_.sayHi(); }
private:
class B {
public:
void sayHi() { std::cout << "hola!"; }
};
B member_;
};
Addendum re: comment: The implementation can be separated from the declaration like this:
Header:
class A {
public:
void letBSayHi();
private:
class B {
public:
void sayHi();
};
B member_;
};
Source file:
void A::letBSayHi() { member_.sayHi(); }
void A::B::sayHi() { std::cout << "hola!\n"; }
// ^^^^-- interesting part here
Well, if you want to include, why not?
class A {
#include "B.hpp"
...
};

How to make these c++ class declarations work

I'm currently reading C++ Primer and am at the point of class friends and member function friends and I'm having trouble figuring out how to get the code that has the following pseudoform to work:
class B;
class A {
public:
A(int i): someNum(i) { }
private:
int someNum;
friend void B::someMemberFunction(Args); // Compile error: Incomplete Type
};
class B {
public:
void someMemberFunction(Args) { /* doStuff */ }
private:
vector<A> someVector { A(5) };
};
If you try to compile in this form it gives the incomplete type error on the friend line. So the solution is to move the class B definition above class A:
class A;
class B {
public:
void someMemberFunction(Args) { /* doStuff */ }
private:
vector<A> someVector { A(5) }; // Compile error: Incomplete Type
};
class A {
public:
A(int i): someNum(i) { }
private:
int someNum;
friend void B::someMemberFunction(Args);
};
However now on the vector line, it doesn't know how to create an object of type A, since A has yet to be defined. So then A needs to be defined before B. But now we've arrived back at the original problem. I think this is called circular dependency? I don't know how to fix this with forward declarations.
Any help would be appreciated. Thanks.
I think you will either have to make the whole of class B a friend (which removes a dependency in A anyway so it's probably a good thing), or use a constructor instead of the in-class initializer.
class B;
class A {
public:
A(int i): someNum(i) { }
private:
int someNum;
friend class B;
};
class B {
public:
void someMemberFunction() { /* doStuff */ }
private:
vector<A> someVector { A(5) };
};
Or this
class A;
class B {
public:
B();
void someMemberFunction() { /* doStuff */ }
private:
vector<A> someVector;
};
class A {
public:
A(int i): someNum(i) { }
private:
int someNum;
friend void B::someMemberFunction();
};
B::B(): someVector{A(5)} { }

Will static keyword helps here

class A{
int _a;
public:
/--/ void setfunc(int a) ............. will static works here
{
_a=a;
}
int getValue(){return _a};
};
class B{
public:
void func()
{
/--/ setfunc(1); ...................Dont want to create object of A.
}
};
class C{
public:
void something()
{
A aa;
cout<<aa.getValue(); ............. want a value update by class B setfunc
}
};
int main()
{
B bb;
bb.func();
C cc;
cc.something();
}
Question : How can setfunc() can be called in another function without using that class object. Also, if it changes like setting value of "_a" via someclass B. the same value of will persist whenever I try to retrieve it in someanother class like C via getValue()
In static function you can use only static members of class. Like this (_a is static):
class A {
static int _a;
public:
static void setfunc(int a)
{
_a=a;
}
static int getValue(){return _a};
};
Otherwise, you can't do anything with non-static members:
class A {
int _a;
public:
static void setfunc(int a)
{
_a=a; // Error!
}
static int getValue(){return _a}; // Error!
};

access member in different classes

I have 2 classes such as below:
class A : public C
{
void Init();
};
class B : public C
{
int a = 3;
};
How can i access the member of class B in class A?
class A::Init()
{
class B. a ???
}
Perhaps you want a static member, which you can access without an object of type B?
class B : public C
{
public:
static const int a = 3;
};
Now you can access it from anywhere (including your A::Init function) as B::a.
If that's not what you want, then please clarify the question.
You must create an instance of the class B and declare the variable public
class B : public C
{
public:
int a=3;//Data member initializer is not allowed in Visual Studio 2012, only the newest GCC
};
void A::Init()
{
B b;
b.a= 0;
}
If you don't want to create an instance of class B, declare the variable static
class B : public C
{
public:
static int a = 3;
};
And then access it like this:
void A::Init()
{
B::a=0;
}
if you want access a "property" of class B as global, not a particular object then make variable static in this class
class B : public C
{
public:
static const int a = 0;
};
and access it using B::a
alternatively:
class A{
public:
void init(){
static int a2 = 0;
a1=a2;
}
int a(){return a1;}
private:
static int a1;
};
int A::a1=0;
You can also try to add static function in your class. But I'm not sure if this what you
are looking for.
class B: public C
{
public:
static int GetA(void) {return a;}
private:
static int a;
};
int B::a = 4;
class A: public C
{
public:
void Init()
{
std::cout<<"Val of A "<<B::GetA()<<std::endl;
}
private:
int b;
};