Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to access public members of the class containing the object

Posted on 2008-06-13
10
206 Views
Last Modified: 2013-11-12
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...
0
Comment
Question by:pamir
  • 6
  • 3
10 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 125 total points
ID: 21776981
You need to (a) reorder a little and (b) forward declare.
http://www.devx.com/tips/Tip/12583
class AClass;
 
class BClass
{
public:
	AClass* m_AClass;
	void Test();
};
 
class AClass
{
public:
	BClass m_BClass; // impossible!
 
	AClass()
	{
		m_BClass.m_AClass = this;
	}
 
	int test;
};
 
void BClass::Test()
{
	int test = m_AClass->test;
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 21776987
^^^Oops, I forgot to remove the "// impossible!" from the code I reordered -- ignore it, it is possible :)
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 21777345
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.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:pamir
ID: 21778086
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;
}
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21778120
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.
0
 

Author Comment

by:pamir
ID: 21778122
P.S. the error is in the line
 test = m_AClass->test;
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21778133
^^^ oh, and "#pragma once" isn't portable either, prefer to use include guards.
http://en.wikipedia.org/wiki/Include_guard
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21778146
>> 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

0
 

Author Comment

by:pamir
ID: 21778246
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?...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21778266
>> #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 :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Software development teams often use in-memory caches to improve performance. They want to speed up access to, or reduce load on, a backing store (database, file system, etc.) by keeping some or all of the data in memory.   You should implement a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question