Click here to Skip to main content
11,496,146 members (750 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C++
Hi all,
There are two classes class1 and class2
CV3 Value1;
//how to access the variable Value1 here?

class1* temp;
//this gives initialisation error;
Posted 18-Jul-12 3:03am
//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;

or this:
class1 *temp = new class1;
Member 8446342 at 18-Jul-12 8:36am
Thanks, was being a lil silly :P
A possible variant Smile | :) :
class A
  int m_iVar;
  A(int iVar) : m_iVar(iVar) {}
  int GetVar() const { return m_iVar; }
class B
  A* m_pcA;
  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);
Why doing all? just inherit class A as parent to class B. so you can access class A public variable using class B object
Also u can make class2 as a friend class of class1 to access members of it.
Albert Holguin at 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)

  Print Answers RSS
0 RyanDev 230
1 Sergey Alexandrovich Kryukov 114
2 Richard Deeming 100
3 OriginalGriff 89
4 Mostafa Asaduzzaman 59
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 19 Jul 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100