Click here to Skip to main content
15,912,977 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Choosing libraries for video project Pin
Code-o-mat5-Feb-11 6:19
Code-o-mat5-Feb-11 6:19 
QuestionHow to display jpg image in the CView Pin
yu-jian4-Feb-11 22:30
yu-jian4-Feb-11 22:30 
AnswerRe: How to display jpg image in the CView Pin
Code-o-mat4-Feb-11 23:03
Code-o-mat4-Feb-11 23:03 
AnswerRe: How to display jpg image in the CView Pin
Niklas L6-Feb-11 11:49
Niklas L6-Feb-11 11:49 
Questionc++ classes :( [SOLVED] Pin
csrss4-Feb-11 22:23
csrss4-Feb-11 22:23 
AnswerRe: c++ classes :( Pin
Andrew Brock4-Feb-11 23:28
Andrew Brock4-Feb-11 23:28 
GeneralRe: c++ classes :( Pin
csrss5-Feb-11 0:28
csrss5-Feb-11 0:28 
AnswerRe: c++ classes :( [SOLVED] [modified] Pin
Stefan_Lang7-Feb-11 2:49
Stefan_Lang7-Feb-11 2:49 
While your inquiry is already marked as solved, I wonder if you are really doing the right thing.

First of all, why use a nested class? I've never ever seen a problem that would require such a construction. For the purpose of reusability and information hiding, defining these two classes seperately would always be the better solution. If there are circular dependencies you can get around them with forward declarations.

Second, if you want to access a variable from another class, the best way is to write an accessor for it, rather than making the variable public.

Third, what you described last, i. e. inheriting your Child class form the Parent class, was wrong for at least two reasons: first, an instance of the Child class is not a Parent, and that would be what inheritance would imply. Second, the Parent instance would become part of every child instance, so you'd have an individual Parent object for each child, and each would only have this particular child that it is part of - that is why you always got a==1.

Fourth, one of your ideas was to use static variables. But if you do that, you can only ever have one parent object. If that is what you want, and you are sure you will never ever need several different parents, each with their own children, then you can make Parent a singleton class. You could then solve your problem like that:

// Parent.h
class Parent {
private:
   int sum;
   Parent() : sum(0) {} // private constructor prevents accidental creation of additional parents
   Parent(const Parent&) {} // same for copy constructor
   Parent& operator=(const Parent&) {} // same for assignment
public:
   static Parent* get();
   void work(const class Child& child); // use forward declaration for (yet) undefined class Child
   int sum() const { return sum; } // get some results
};

// Child.h
class Child {
private:
   int x; // some data
public:
   Child(int a);
   int value() const { return x; }
};

// Parent.cpp
#include "Parent.h"
#include "Child.h"
static Parent* theParent = 0;
Parent* Parent::get() {
   if (theParent==0)
      theParent = new Parent;
   return theParent;
}
Parent::work(const Child& child) {
   // do something
   sum += child.value();
}

// Child.cpp
#include "Child.h"
#include "Parent.h"

Child::Child(int a) : x(a) {
   Parent* parent = Parent::get();
   parent->work(*this);
}

// main.cpp
#include "Child.h"
#include "Parent.h"

void test() {
   Child abe(3), bert(4), chris(5);
   printf("sum=%d\r\n", Parent::get()->sum()}; // prints "sum=12"
}


This implementation may also use the Child class without ever referring to the Parent class, and vice versa, i. e. you could produce children in one module and query the Parent in another, and you'd still get correct results.

[edit]Added missing implementation for work[/edit]

modified on Monday, February 7, 2011 8:55 AM

QuestionHow can text formatting tool in EditBox? Pin
Le@rner4-Feb-11 19:37
Le@rner4-Feb-11 19:37 
AnswerRe: How can text formatting tool in EditBox? Pin
Andrew Brock4-Feb-11 20:04
Andrew Brock4-Feb-11 20:04 
Questionprogram whit c language Stepper Motor Control through Parallel Port. help Pin
saleheh4-Feb-11 19:10
saleheh4-Feb-11 19:10 
AnswerRe: program whit c language Stepper Motor Control through Parallel Port. help Pin
Andrew Brock4-Feb-11 19:56
Andrew Brock4-Feb-11 19:56 
GeneralRe: program whit c language Stepper Motor Control through Parallel Port. help Pin
saleheh4-Feb-11 21:15
saleheh4-Feb-11 21:15 
Questionc++ split fnction help Pin
ehsan161804-Feb-11 19:02
ehsan161804-Feb-11 19:02 
AnswerRe: c++ split fnction help REPOST Pin
Richard MacCutchan4-Feb-11 23:38
mveRichard MacCutchan4-Feb-11 23:38 
QuestionHow to add items to CListCtrl faster? Pin
includeh104-Feb-11 10:53
includeh104-Feb-11 10:53 
AnswerRe: How to add items to CListCtrl faster? Pin
Hans Dietrich4-Feb-11 11:24
mentorHans Dietrich4-Feb-11 11:24 
GeneralRe: How to add items to CListCtrl faster? Pin
Chris Meech4-Feb-11 12:16
Chris Meech4-Feb-11 12:16 
GeneralRe: How to add items to CListCtrl faster? Pin
includeh104-Feb-11 13:57
includeh104-Feb-11 13:57 
GeneralRe: How to add items to CListCtrl faster? Pin
Hans Dietrich4-Feb-11 14:25
mentorHans Dietrich4-Feb-11 14:25 
AnswerRe: How to add items to CListCtrl faster? Pin
User 74293384-Feb-11 11:56
professionalUser 74293384-Feb-11 11:56 
AnswerRe: How to add items to CListCtrl faster? Pin
Andrew Brock4-Feb-11 15:09
Andrew Brock4-Feb-11 15:09 
QuestionHow to Run a thread after the process exit Pin
pandit844-Feb-11 2:10
pandit844-Feb-11 2:10 
AnswerRe: How to Run a thread after the process exit Pin
Rajesh R Subramanian4-Feb-11 2:57
professionalRajesh R Subramanian4-Feb-11 2:57 
GeneralRe: How to Run a thread after the process exit Pin
Stephen Hewitt4-Feb-11 3:21
Stephen Hewitt4-Feb-11 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.