Will static keyword helps here - c++

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!
};

Related

How to access private members of a nested class?

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; }
};

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)} { }

C++: Call child static method from parent

Simply, I need to do as the title says: Call a child static method from parent. The problem is that I don't know the child class name in the parent as there could be multiple children. The static method needs to stay static. For example:
class A{ // parent class
public:
void process(){
getData(); // <-- Problem
}
}
class B: public A{ // child class
static int getData();
}
void main(){
B b;
b.process();
}
One solution that comes to mind is to have a virtual method that calls the static method. This would not be very nice and it would mean I would have to implement the method for every child I have:
class A{ // parent class
virtual int getDataFromStaticMethod() = 0;
public:
void process(){
getData(); // <-- Problem
}
}
class B: public A{ // child class
static int getData();
virtual int getDataFromStaticMethod(){
return B::getData();
}
}
void main(){
B b;
b.process();
}
But I really wish it was possible to implement a pure virtual method with a static method:
class A{ // parent class
virtual int getData() = 0;
public:
void process(){
getData(); // <-- Problem
}
}
class B: public A{ // child class
static int getData();
}
void main(){
B b;
b.process();
}
Any suggestions?
Thanks!
You could use templates.
template<typename TChild>
class A
{
typedef TChild child_type;
public:
void process()
{
child_type::getData();
}
};
class B: public A<B>
{
static int getData();
};
class C: public A<C>
{
static int getData();
};
int main(int argc, char** argv)
{
B b;
b.process();
C c;
c.process();
}
Note:
If you want to hold static state in your base class, or if you need to hold a collection of base class objects, then you would need an additional layer:
class ABase
{
//any static state goes here
public:
virtual int process() = 0;
};
template<typename TChild>
class A: public ABase
{
typedef TChild child_type;
public:
int process()
{
child_type::getData();
}
};
class B: public A<B>
{};
std::vector<ABase*> a_list;
Assuming the signature of the child functions are all identical, you could initialize your base class object to hold a pointer to the child's version of the getData() function, e.g.:
class A {
int (*d_getData)();
protected:
explicit A(int (*getData)()): d_getData(getData) {}
public:
void process() {
int data = (this->d_getData)();
// ...
}
};
Obviously, the child classes would need to provide the corresponding constructor argument:
class B: public A {
static int whatever();
public:
B(): A(&whatever) {}
// ...
};
That's sort of an implementation of a per object overridable virtual function.
You can use virtual wrappers around the static function. E.g.
class A{ // parent class
// don't make pure virtual if you don't want to define it in all child classes
virtual int getData() { return 0 };
public:
void process(){
getData();
}
}
class B: public a{ // child class
static int do_getData();
int getData() { return do_getData(); }
}

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;
};

How to overwrite a virtual method defined in a class in another class which is inside a function in C++

I have a class A which is in a separate file(sayfile1.cpp)
class A{
public:
virtual int add(){
int a=5;
int b=4;
int c = a+b;
return c;
}
};
Now in a different file(say file2.cpp), i have a function(i have a many other things in this function) inside which i want to create a class inherited from class A and implement the virtual method declared in class A.
void function(Mat param1, Mat param2)
{
//Some process here..
..
..
int c=100;
class B:public A{
public:
virtual int add(){
return c;
}
};
}
Now if i were to call the function int add(), i want the result of c to be 100 and not 9.
Is it possible to do something like this in C++ ??
Thanks in advance
Define member variable:
class B: public A {
int c_;
public:
explicit B(int c):c_(c){};
virtual int add() {
return c_;
}
}
B variable((100));
You need to split your file1.cpp into file1.h:
#ifndef FILE1_H
class A {
public:
virtual int add();
};
#endif
and file1.cpp with it's implementation:
int A::add { /*your code * }
In the other file you include only the header file:
#include "file1.h"
The following is not legal in C++:
void function(Mat param1, Mat param2)
{
//Some process here..
..
..
int c=100;
class B:public A {
public:
virtual int add(){
return c;
}
};
}
Instead, you need something like this:
class B : public A {
public:
B(int v) : c(v) {}
virtual int add(){ return c; }
private:
int c;
};
void function(Mat param1, Mat param2)
{
//Some process here..
..
..
int c=100;
B o(c);
}