Click here to Skip to main content
15,566,065 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
There are two classes class1 and class2
C++
class1()
{
public: 
CV3 Value1;
}

class2()
{
//how to access the variable Value1 here?
}


C++
class1* temp;
temp->variable.x; 
//this gives initialisation error;
Posted

//this gives initialisation error;

of course it does. you haven't actually created an instance of an object of type class1. you created a pointer to an object, and they you didn't point it to anything.

try this:
class1 temp;
temp.variable.x; 


or this:
class1 *temp = new class1;
temp->variable.x; 
 
Share this answer
 
Comments
Sumal.V 18-Jul-12 8:36am    
Thanks, was being a lil silly :P
A possible variant :) :
C++
class A
{
  int m_iVar;
public:
  A(int iVar) : m_iVar(iVar) {}
  int GetVar() const { return m_iVar; }
};

class B
{
  A* m_pcA;
public:
  B(A* pcA) : m_pcA(pcA) { ASSERT(m_pcA); }
  int Test() const { return m_pcA ? m_pcA->GetVar() : 0; }
}

void Tast()
{
  enum { testVar = 3 };
  A a(testVar);
  B b(&a);
  int iResult(b.Test());
  ASSERT(testVar == iResult);
}
 
Share this answer
 
Why doing all? just inherit class A as parent to class B. so you can access class A public variable using class B object
 
Share this answer
 
v2
Also u can make class2 as a friend class of class1 to access members of it.
 
Share this answer
 
Comments
Albert Holguin 19-Jul-12 10:51am    
This is only applicable to accessing private members. You also need to already have the object instantiated somehow.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900