Solved

Avoiding class reference (pointer) crossing

Posted on 1998-06-18
10
152 Views
Last Modified: 2013-11-20
This one should be simple and reminds me of the cross referencing problem notorious in the Assembler world. I'd like to find a more elegant solution to the problem, which as luck would have it I've seldom come across.

Dialog_A uses functions and variables in Dialog_B and Dialog_B uses functions in Dialog_A. If I do the following, for maximum scope, the compiler screams:

Dialog_A.h
#include "Dialog_B.h"
Dialog_B pB;

Dialog_B.h
#include "Dialog_A.h"
Dialog_A pA;

Compile one or the other cpp and the compiler will complain.

I hope you have some good suggestions on avoiding what should be a simple problem.

RJV
0
Comment
Question by:RJV
  • 5
  • 3
  • 2
10 Comments
 
LVL 2

Accepted Solution

by:
Belgarat earned 70 total points
Comment Utility
The standard workaround is this:

"dialog_a.h"
#ifndef __dialog_a_h__
#define __dialog_a_h__

#include "dialog_b.h"
<everything you want>

#endif
------

"dialog_b.h"
#ifndef __dialog_b_h__
#define __dialog_b_h__

#include "dialog_a.h"
..

#endif
---------

This way contents of either of the headers will not be #included more than once
0
 

Author Comment

by:RJV
Comment Utility
Belgarat,

That's what I would have imagined, but no, that does not solve the problem. In fact, Visual C++ 5.0 includes this type of definition in every single header file, together with the product serial number.

RJV

0
 
LVL 2

Expert Comment

by:Belgarat
Comment Utility
What error messages are you receiving from the compiler ?
0
 

Author Comment

by:RJV
Comment Utility
These three:

Dialog_B.h(108) : error C2501: 'Dialog_B' : missing decl-specifiers
Dialog_B.h(108) : error C2146: syntax error : missing ';' before identifier 'pA'
Dialog_B.h(108) : error C2501: 'pA' : missing decl-specifiers

Comment out 'pA' in Dialog_B.h and all's okay.

RJV

0
 
LVL 5

Expert Comment

by:yonat
Comment Utility
put a forward declaration
    class Dialog_A;
before Dialog_B and vice versa. Move the #include-s to the cpp files.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:RJV
Comment Utility
Have you tried that? You should get this error:

   error C2079: 'pA' uses undefined class 'Dialog_A'

This from compiling Dialog_B and leaving 'Dialog_A pA;' in the header file. I had suspected that as the necessary #include was removed from the header file.

Note that when I posted the question I had solved the problem by leaving the #include in the header file of Dialog_A; namely, unchanged from the first example I posted here. I removed 'Dialog_A pA' and the #include for Dialog_A from Dialog_B's header file and placed both in the cpp file. I put 'Dialog_A pA' outside the class (i.e. up top), thereby making that a global variable. I don't think this is the most elegant solution, though, which is why I came here.

RJV

0
 
LVL 5

Expert Comment

by:yonat
Comment Utility
Oh, I didn't read carefully - I assumed pA was a *pointer* to a Dialog_A. Ofcourse there is no way for a Dialog_B a to contain an actual Dialog_A and vice versa: Just imagine calculating the size of such an object:
sizeof (Dialog_A) = x + sizeof(Dialog_B) and sizeof (Dialog_B) = x + sizeof(Dialog_A) !

You should make each class hold a *poinetr* to the other class, not an actual object.

0
 

Author Comment

by:RJV
Comment Utility
I'm glad you grasped it! BTW, is there any difference in the demand on memory from the pointer to holding the object? I ask as most of the very powerful MFC toolkits don't use pointers, but the object method.

RJV

0
 
LVL 5

Expert Comment

by:yonat
Comment Utility
A pointer has the same size, regardless of what it is pointing too (OK, there are exceptions, I know). So, if you use a pointer, you get
sizeof (Dialog_A) = x + sizeof(pointer) and sizeof (Dialog_B) = y + sizeof(pointer)
no infinity there.

> I ask as most of the very powerful MFC toolkits don't use pointers

Huh? You use value containment when appropriate, and pointer when appropriate. There is no rule that sais "don't use pointers". For example, if you use value containment, then the lifetime of the contained object is limited by the liftime of it's container. If this is not what you want, then value containment will be wrong.

Maybe you should state the problem you are trying to solve (ie, *why* do you need Dialog_A in Dialog_B and vice versa) and then one of the experts will be able to offer better advice.
0
 

Author Comment

by:RJV
Comment Utility
You can nearly always use one or the other, through pointers are generally obtainable at any time. However, the scope or lifetime of the pointer is just as limited as you desire, as with object referencing.

The why one needs to access the data of the other is due to:

   1. Scope or lifetime of the object
   2. Both are dialogs, as indicated. The child dialog needs
      data of its parent. The parent simply must have access
      to the child. The scope or lifetime must include all
      functions of the parent.

RJV
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sameEnds challenge 3 105
ffmpeg - "rtsp://...... Operation not permitted" 4 68
isEverywhere  challenge 19 64
canBalance challenge 34 65
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now