Link to home
Start Free TrialLog in
Avatar of pamir
pamir

asked on

How to access public members of the class containing the object

Hi experts,

Is it possible in C++ to access public members of the class containing the object?

For example, in this way it is impossible:

class AClass
{
public:
      BClass m_BClass; // impossible!
      m_BClass->m_AClass = this;
      
      int test;
}

class BClass
{
public:
      AClass* m_ AClass;
      void Test();
}

void BClass::Test()
{
      int test = m_ AClass->test;
}

I am asking that because in Java it is possible, since there classes are grouped in packages...
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
^^^Oops, I forgot to remove the "// impossible!" from the code I reordered -- ignore it, it is possible :)
Class is blue print of an object,You need to write this in a public fuction
<return type> <some function name>()
{
m_BClass.m_AClass = this;
}

or in a constuctor.
Avatar of pamir
pamir

ASKER

Thanks evilrix,
I see that line 1 in your code is doing the trick.

Howerver, in my real situation, I am using header files and I am receiving an error:
BClass.cpp(14) : error C2027: use of undefined type 'AClass'

So I am wondering what is wrong here?

The code is:

AClass.h:
=======================================================================
#pragma once
#include ".\bclass.h"

class AClass
{
public:
      AClass(void);
      ~AClass(void);

      BClass m_BClass;
      int test;
};


AClass.cpp:
=======================================================================
#include "StdAfx.h"
#include ".\aclass.h"

AClass::AClass(void)
{
      m_BClass.m_AClass = this;
}

AClass::~AClass(void)
{
}


BClass.h:
=======================================================================
#pragma once

class AClass;

class BClass
{
public:
      BClass(void);
      ~BClass(void);

      AClass* m_AClass;
      int test;

      void Test();
};


BClass.cpp:
=======================================================================
#include "StdAfx.h"
#include ".\bclass.h"

BClass::BClass(void)
{
}

BClass::~BClass(void)
{
}

void BClass::Test()
{
      test = m_AClass->test;
}
You need to include aclass.h in bclass.cpp

The forward declaration only allows you to define a pointer or reference, to use it the compiler must see the full class definition.

BTW (off topic): Don't prefix  .\ to file names when including headers, it's not portable. Since the headers are in the same folder as the .cpp files just use "aclass.h". If you do need to use a path you should use / and not \ as the / char is portable on both Windows and Linux where as \ is a Windows only seperator.
Avatar of pamir

ASKER

P.S. the error is in the line
 test = m_AClass->test;
^^^ oh, and "#pragma once" isn't portable either, prefer to use include guards.
http://en.wikipedia.org/wiki/Include_guard
>> You need to include aclass.h in bclass.cpp
See below.

AClass.h:
=======================================================================
#pragma once
#include ".\bclass.h"
 
class AClass
{
public:
      AClass(void);
      ~AClass(void);
 
      BClass m_BClass;
      int test;
};
 
 
AClass.cpp:
=======================================================================
#include "StdAfx.h"
#include ".\aclass.h"
 
AClass::AClass(void)
{
      m_BClass.m_AClass = this;
}
 
AClass::~AClass(void)
{
}
 
 
BClass.h:
=======================================================================
#pragma once
 
class AClass;
 
class BClass
{
public:
      BClass(void);
      ~BClass(void);
 
      AClass* m_AClass;
      int test;
 
      void Test();
};
 
 
BClass.cpp:
=======================================================================
#include "StdAfx.h"
#include ".\bclass.h"
#include "abclass.h" // <--- LOOK !!!
 
BClass::BClass(void)
{
}
 
BClass::~BClass(void)
{
}
 
void BClass::Test()
{
      test = m_AClass->test;
}

Open in new window

Avatar of pamir

ASKER

Yes, it works, the forward declarations are very useful, thanks a lot!

OK, I have read the include guards article, and also the article for #pragma once http://en.wikipedia.org/wiki/Pragma_once - but it seems that #pragma once does the work with less code?...
>> #pragma once does the work with less code?...
It does but it is NOT part of the C/C++ standard, it is a Microsoft only extension. As long as you know this and are happy to accept your code will not be portable then use it as your wish but if you need to write code to compile on other platforms then you cannot use it.

It was just an advisory :)