accesing a class variable in other class function C++ - c++

i have a variable map dataa ehich is used in three different class.i can define this variable globally and put all classes defination in one cpp file but i want to make different files for three different class and thus cant define it globally.
now i want to define this variable in one class say A and then want to use this dataa in the rest two class say B and C.
how can i do this.thanks for anyhelp in advance

You can use public get and set methods to access your variable in class A. Or simply create a public variable in class A.

You can use friends
class A
{
friend class B;
friend class C;
private:
int m_privateMember;
};
class B {
};
class C {
};
Now, B and C can access to A's private members.
But this is not the best way. Try to avoid it.

alternatively you can try to have this variable map data as part of a new singleton class. The rest of your 3 different classes can access this singleton class using get method
file: singleton.h
#include <iostream>
using namespace std;
class singletonClass
{
public:
singletonClass(){};
~singletonClass(){};
//prevent copying and assignment
singletonClass(singletonClass const &);
void operator=(singletonClass const &);
//use this to get instance of this class
static singletonClass* getInstance()
{
if (NULL == m_Singleton) //if this is the first time, new it!
m_Singleton = new singletonClass;
return m_Singleton;
}
int getData()
{
return data;
}
void setData(int input)
{
data = input;
}
private:
static singletonClass* m_Singleton; //ensure a single copy of this pointer
int data;
};
//declare static variable as NULL
singletonClass* singletonClass::m_Singleton = NULL;
File: ClassA.h
class ClassA
{
public:
ClassA(){};
~ClassA(){};
int getVarFromSingleton()
{
m_Singleton = singletonClass::getInstance(); //get a pointer to the singletonClass
return data = m_Singleton->getData(); //get data from singleton class and return this value
}
private:
singletonClass* m_Singleton; //declare a pointer to singletonClass
int data;
};
File: main.cpp
int main()
{
singletonClass* DataInstance;
ClassA a;
int data;
DataInstance = singletonClass::getInstance();
DataInstance->setData(5);
data = a.getVarFromSingleton();
cout << "shared data: " << data << endl;
return 0;
}

Related

Query on Static member variables of a class

I have few classes defined in this fashion :
class CSocket {
protected:
int m_SockWorking;
int InitSocket();
void CleanSocket();
}
class CSocketClient : public CSocket {
public:
CSocketClient();
int InitClient();
}
class CApi {
static CSocketClient c1;
public:
// other methods defined
}
Now, in the main routine I create two objects of class CApi. I know that for static member variables there exists only one copy per class. But, how can I access m_SockWorking variable for CSocketClient member variable? Will there be two copies of that non static member variable for the two objects? Can somebody please explain?
All CApi classes will share a single static CSocketClient variable.
m_SockWorking is a protected member of CSocket which is inheirited by CSocketClient this makes it a private variable inside the context of CApi and your application. So you can access it through the use of accessor and mutator functions.
Each CSocketClient has its own m_SockWorking variable. Since all CApi all share the same CSocketClient class, they all share the same m_SockWorking variable.
CSocket.h
class CSocket {
protected:
int m_SockWorking;
int InitSocket();
void CleanSocket();
public:
void setSocket(int val){ m_SockWorking = val; } /* added these functions to show how to accessing m_SockWorking can be done */
int getSocket() const { return m_SockWorking; }
};
class CSocketClient : public CSocket {
public:
CSocketClient();
int InitClient();
};
class CApi {
static CSocketClient c1;
public:
// other methods defined
};
main.cc
#include "CSocket.h"
#include <iostream>
using std::cout;
using std::endl;
int main(){
CApi a, b;
CSocketClient c2;
a.c1.setSocket(0); /* sets static c1 m_SockWorking variable to 0 */
cout << b.c1.getSocket() << endl; /* will print 0 since c1 is a static variable */
c2.setSocket(1); /* set c2 m_SockWorking to 1 */
cout << c2.getSocket() << endl; /* will print 1 */
cout << a.c1.getSocket() << endl; /* will print 0 */
}
Output
0
1
1
Yes, object c1 is unique, so, all data members within to c1 are unique for CApi.
Maybe it's less confusing in this way:
static object c1 is a standalone & unique object for the "CLASS" CApi, accessible by objects of CApi. It belongs to the class, NOT to any objects of CApi; it's NOT A SHARED PART of objects of CApi.

Private static class members

When we declare a member variable static, it is shared between all instances of the class. I've heard that you should think of the variable belonging to the class itself, not any instance. This lets us initialize the variable without instantiating any object of the class, which makes sense.
class Something
{
public:
static int s_nValue;
};
int Something::s_nValue = 1;
But why are we allowed to initialize a private static member?
class Something
{
private:
static int s_nValue;
};
int Something::s_nValue = 1;
Does private even mean anything when we are talking about static members?
Yes, it does mean something. Consider the following example, which throws a compiler error, because the member is private. Being able to initialize a private variable is not the same as being able to change it from any context.
class Something
{
private:
static int s_nValue;
};
int Something::s_nValue = 1;
int main(){
Something::s_nValue = 2; // Compiler error here.
}
Private still means the same thing: you cannot use the name Something::s_nValue except in the definition of a member of Something (or a friend, or a nested class within Something).
int Something::s_nValue = 1;
is the definition of a member of Something - namely, that static member s_nValue.
int Something::another_static_val = s_nValue; // also okay
int OtherClass::x = Something::s_nValue; // Illegal access!
int Something::getValue() const {
return s_nValue; // okay, getValue is a member of same class
}
int regularFunction() {
return Something::s_nValue; // Illegal access!
}
Does private even mean anything when we are talking about static members?
I'll try to answer with a classic example. Consider the following piece of code:
#include <iostream>
class foo {
static int count;
int id;
public:
foo() : id(++count) {}
int getid() const { return id; }
};
int foo::count = 0;
int main() {
foo f1, f2, f3;
std::cout << f1.getid() << std::endl;
std::cout << f2.getid() << std::endl;
std::cout << f3.getid() << std::endl;
}
LIVE DEMO
In the example above we use a private static int to count the instances of foo created. We made the count static member variable private because we don't want anyone else except object of type foo to mess with it.
And this is only a naive example, think of the possibilities.
Public, private and protected are properties of a class and not of an object. Their purpose is to let you specify which parts of this class are visible to other classes, and not to hide stuff from objects of the same class. So, you can write code like this :
class A
{
public:
bool operator<(const A& other)
{
return this->val < other.val;
}
private:
int val;
};
So, private makes sense even when applied to static members - it just says that other classes cannot see this member.

Access static const in another class.

A static const variable declared and defined in a class. How to access it in the private access of another class in same project. Is it possible?
//in some header file
Class A{
public:
//some data
private:
static const uint8_t AVar =1;
//other data
};
//in some another header file
Class B{
static const Bvar;
};
//here inside Class B it possible to give Bvar = AVar ? If yes, How ?
A clean way to avoid duplication of the magic value without weakening encapsulation of either class is to move the magic value to a different place that is publicly accessible to both classes.
For example:
namespace detail {
enum MAGIC_NUMBER_T {
MAGIC_NUMBER = 1
};
}
class A{
private:
static const uint8_t AVar = detail::MAGIC_NUMBER;
};
class B{
static const uint8_t BVar = detail::MAGIC_NUMBER;
};

Defining two classes inside a class, and accessing its members

I'm trying to access members of a classC from classB, both classC and classB are inside classA. Here is what I'm trying to do;
//hello.h
class hello{
public:
hello();
class letters{
public:
letters();
void setName(char n);
char getName();
private:
char name;
}
class sayHi{
public:
sayHi();
void onJoin();
}
}
//hello.cpp
hello::hello(){}
hello::letters(){}
hello::sayHi(){}
void hello::letters::setName(char n){
hello::letters::name = n; //trying to access the private variable 'name' inside class letters
}
char hello::letters::getName(){
return hello::letters::name = n;
}
void hello::sayHi::onJoin(){
cout<< hello::letters::getName() <<endl;
}
I know i'm doing it wrong, am I supposed to create instances of each class and call the members?
Yes, you're supposed to create instances of the classes.
These are frequently called "objects", which is why they call it "object-oriented programming".
First, your getName and setName should look like this:
void hello::letters::setName(char n) {
name = n;
}
char hello::letters::getName() const { // Declaration should also say "const".
return name;
}
With that out of the way, any sayHi instance needs to know which letters to say "Hi" to, which means that you need to tell it.
This is usually done by passing a parameter to the method that needs to know:
class sayHi{
public:
sayHi();
void onJoin(const letters& who)
{
cout << who.getName() << endl;
}
};
which you would use somewhat like this:
int main()
{
hello::letters letter;
letter.setName('p');
hello::sayHi greeter;
greeter.onJoin(letter);
}
What is the error you are getting ? where have you created objects for accessing these methods ? Also
return hello::letters::name = n;
this line is wrong, it should be
return hello::letters::name;

How access class variables in c++

Is it possible in c++ to access class variables in other classes without creating an object. I have tried to use static, but the other class doesnt recognize my variable.
I have 3 classes. In two of those the sae variables should be used. In the third class I am changing the values. Would be grateful if you could help. Maybe youve got an example.
class Myclass
{
public:
static int i;
};
int Myclass::i = 10;
class YourClass
{
public:
void doSomething()
{
Myclass::i = 10; //This is how you access static member variables
}
};
int main()
{
YourClass obj;
obj.doSomething();
return 0;
}
static is the right keyword here:
class A {
public:
static int i; // <-- this is a class variable
};
class B {
public:
void f() { A::i = 3; } // <-- this is how you access class variables
};
They only potential problem I can think of is that
You made the class variable protected or private, thus rendering it inaccessible from other code.
You forgot to specify the full scope of the class variable (with A:: in this example).
I think the Singleton Pattern would help, but I'm no big fan of it. A lot better design would be to have one class take ownership of the object, and pass references to this object to the other classes.
yes you can bro, try this
struct car{
string model;
string paint;
int price;
};
int main(){
// creates an object of the class car
car BMW;
// assign the values
bmw.model = "m sports";
bmw.paint ="red";
bmw.price = 24000;
}