Accessing child class variables from its parent class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite
1












Requirements



The code should be able to call a parent method to read and modify child object variable that is referenced by a pointer inside the Child class. This code is to be run on an embedded environment, so memory and performance requirements must be addressed.



Application



The proposed solutions allows a parent class to have access to the child class variables. If for an example we have a class Player that inherits classes Stats and Events. The class Player loads stats using methods from class Stats. Then when an event occurs it will trigger a method inside Events class which will read and modify stats from a variable that is declared in its child class Player.



Solution 1



Using a virtual method in a parent class and implement it in a child class will allow it to be called in a parent class and in-turn be able to get the required object.



Example A. Using one parent class



#include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
virtual Life* getLife();
void printMeaningOfLife()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class childClass : public parentClassA
public:
Life* A;
childClass()
A = new Life;

Life* getLife()
return this->A;

;

int main()

childClass c;
c.printMeaningOfLife();
std::cout << c.A->meaning << std::endl;
return 0;



Example B. Using multiple parent classes



 #include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
virtual Life* getLife()=0;
void printMeaningOfLifeA()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class parentClassB
public:
virtual Life* getLife()=0;
void printMeaningOfLifeB()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;


;

class childClass : public parentClassA, public parentClassB
Life* A;
public:
childClass()
A = new Life;

Life* getLife()
return this->A;

;

int main()

childClass c;
c.printMeaningOfLifeA();
c.printMeaningOfLifeB();

return 0;



The good



  • Does not create copies of the object

  • Accessing the object requires only one function

The bad



  • N/A

Solution 2



Passing an object pointer by reference to a function that requires it.



#include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
void printMeaningOfLife(Life*& A)
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class childClass : public parentClassA
public:
Life* A;
childClass()
A = new Life;

;

int main()

childClass c;
c.printMeaningOfLife(c.A);
std::cout << c.A->meaning << std::endl;
return 0;



The good



  • Does not create copies of the object

The bad



  • Requires that the pointer be passed on every method between the child class and the accessing location

My conclusion



In my opinion and for my requirements I prefer the first solution. Reason being that I have 5 methods between object and accessing location which would make code readability and maintainability harder. Since the object is only referenced and not copied there should be minimal impact on memory.



The questions



  • Did I make any mistakes?

  • Can this be improved?

  • I could only think of these two solutions. Is there a different/better one?

  • What are other good and bad sides of using these two solutions?

  • Is having a requirement to access a child variable in a parent class a bad design all in itself?

Edit



Since there were no answers I'm assuming that the code and my conclusion is valid. I have also edited the code to use pure virtual functions instead of regular ones to force implementation.







share|improve this question





















  • Is this actual code used in a project, or is it merely hypothetical?
    – Sam Onela
    Jan 5 at 22:08










  • This is the code that I have written to solve the problem that I currently face. If there are no problems with the proposed solutions to the requirements I will implement this into my current project.
    – CodeBreaker
    Jan 5 at 22:21










  • Okay - What task does this code accomplish? Please tell us, and also make that the title of the question via edit. "State what your code does in your title, not your main concerns about it.". Please read How to Ask.
    – Sam Onela
    Jan 5 at 22:23










  • @SamOnela Edits have been made. Unfortunately, my company prevents me to talk and publish code of developing projects so I can't tell what the actual project is and what this solves in it.
    – CodeBreaker
    Jan 5 at 23:05
















up vote
1
down vote

favorite
1












Requirements



The code should be able to call a parent method to read and modify child object variable that is referenced by a pointer inside the Child class. This code is to be run on an embedded environment, so memory and performance requirements must be addressed.



Application



The proposed solutions allows a parent class to have access to the child class variables. If for an example we have a class Player that inherits classes Stats and Events. The class Player loads stats using methods from class Stats. Then when an event occurs it will trigger a method inside Events class which will read and modify stats from a variable that is declared in its child class Player.



Solution 1



Using a virtual method in a parent class and implement it in a child class will allow it to be called in a parent class and in-turn be able to get the required object.



Example A. Using one parent class



#include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
virtual Life* getLife();
void printMeaningOfLife()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class childClass : public parentClassA
public:
Life* A;
childClass()
A = new Life;

Life* getLife()
return this->A;

;

int main()

childClass c;
c.printMeaningOfLife();
std::cout << c.A->meaning << std::endl;
return 0;



Example B. Using multiple parent classes



 #include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
virtual Life* getLife()=0;
void printMeaningOfLifeA()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class parentClassB
public:
virtual Life* getLife()=0;
void printMeaningOfLifeB()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;


;

class childClass : public parentClassA, public parentClassB
Life* A;
public:
childClass()
A = new Life;

Life* getLife()
return this->A;

;

int main()

childClass c;
c.printMeaningOfLifeA();
c.printMeaningOfLifeB();

return 0;



The good



  • Does not create copies of the object

  • Accessing the object requires only one function

The bad



  • N/A

Solution 2



Passing an object pointer by reference to a function that requires it.



#include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
void printMeaningOfLife(Life*& A)
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class childClass : public parentClassA
public:
Life* A;
childClass()
A = new Life;

;

int main()

childClass c;
c.printMeaningOfLife(c.A);
std::cout << c.A->meaning << std::endl;
return 0;



The good



  • Does not create copies of the object

The bad



  • Requires that the pointer be passed on every method between the child class and the accessing location

My conclusion



In my opinion and for my requirements I prefer the first solution. Reason being that I have 5 methods between object and accessing location which would make code readability and maintainability harder. Since the object is only referenced and not copied there should be minimal impact on memory.



The questions



  • Did I make any mistakes?

  • Can this be improved?

  • I could only think of these two solutions. Is there a different/better one?

  • What are other good and bad sides of using these two solutions?

  • Is having a requirement to access a child variable in a parent class a bad design all in itself?

Edit



Since there were no answers I'm assuming that the code and my conclusion is valid. I have also edited the code to use pure virtual functions instead of regular ones to force implementation.







share|improve this question





















  • Is this actual code used in a project, or is it merely hypothetical?
    – Sam Onela
    Jan 5 at 22:08










  • This is the code that I have written to solve the problem that I currently face. If there are no problems with the proposed solutions to the requirements I will implement this into my current project.
    – CodeBreaker
    Jan 5 at 22:21










  • Okay - What task does this code accomplish? Please tell us, and also make that the title of the question via edit. "State what your code does in your title, not your main concerns about it.". Please read How to Ask.
    – Sam Onela
    Jan 5 at 22:23










  • @SamOnela Edits have been made. Unfortunately, my company prevents me to talk and publish code of developing projects so I can't tell what the actual project is and what this solves in it.
    – CodeBreaker
    Jan 5 at 23:05












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





Requirements



The code should be able to call a parent method to read and modify child object variable that is referenced by a pointer inside the Child class. This code is to be run on an embedded environment, so memory and performance requirements must be addressed.



Application



The proposed solutions allows a parent class to have access to the child class variables. If for an example we have a class Player that inherits classes Stats and Events. The class Player loads stats using methods from class Stats. Then when an event occurs it will trigger a method inside Events class which will read and modify stats from a variable that is declared in its child class Player.



Solution 1



Using a virtual method in a parent class and implement it in a child class will allow it to be called in a parent class and in-turn be able to get the required object.



Example A. Using one parent class



#include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
virtual Life* getLife();
void printMeaningOfLife()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class childClass : public parentClassA
public:
Life* A;
childClass()
A = new Life;

Life* getLife()
return this->A;

;

int main()

childClass c;
c.printMeaningOfLife();
std::cout << c.A->meaning << std::endl;
return 0;



Example B. Using multiple parent classes



 #include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
virtual Life* getLife()=0;
void printMeaningOfLifeA()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class parentClassB
public:
virtual Life* getLife()=0;
void printMeaningOfLifeB()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;


;

class childClass : public parentClassA, public parentClassB
Life* A;
public:
childClass()
A = new Life;

Life* getLife()
return this->A;

;

int main()

childClass c;
c.printMeaningOfLifeA();
c.printMeaningOfLifeB();

return 0;



The good



  • Does not create copies of the object

  • Accessing the object requires only one function

The bad



  • N/A

Solution 2



Passing an object pointer by reference to a function that requires it.



#include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
void printMeaningOfLife(Life*& A)
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class childClass : public parentClassA
public:
Life* A;
childClass()
A = new Life;

;

int main()

childClass c;
c.printMeaningOfLife(c.A);
std::cout << c.A->meaning << std::endl;
return 0;



The good



  • Does not create copies of the object

The bad



  • Requires that the pointer be passed on every method between the child class and the accessing location

My conclusion



In my opinion and for my requirements I prefer the first solution. Reason being that I have 5 methods between object and accessing location which would make code readability and maintainability harder. Since the object is only referenced and not copied there should be minimal impact on memory.



The questions



  • Did I make any mistakes?

  • Can this be improved?

  • I could only think of these two solutions. Is there a different/better one?

  • What are other good and bad sides of using these two solutions?

  • Is having a requirement to access a child variable in a parent class a bad design all in itself?

Edit



Since there were no answers I'm assuming that the code and my conclusion is valid. I have also edited the code to use pure virtual functions instead of regular ones to force implementation.







share|improve this question













Requirements



The code should be able to call a parent method to read and modify child object variable that is referenced by a pointer inside the Child class. This code is to be run on an embedded environment, so memory and performance requirements must be addressed.



Application



The proposed solutions allows a parent class to have access to the child class variables. If for an example we have a class Player that inherits classes Stats and Events. The class Player loads stats using methods from class Stats. Then when an event occurs it will trigger a method inside Events class which will read and modify stats from a variable that is declared in its child class Player.



Solution 1



Using a virtual method in a parent class and implement it in a child class will allow it to be called in a parent class and in-turn be able to get the required object.



Example A. Using one parent class



#include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
virtual Life* getLife();
void printMeaningOfLife()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class childClass : public parentClassA
public:
Life* A;
childClass()
A = new Life;

Life* getLife()
return this->A;

;

int main()

childClass c;
c.printMeaningOfLife();
std::cout << c.A->meaning << std::endl;
return 0;



Example B. Using multiple parent classes



 #include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
virtual Life* getLife()=0;
void printMeaningOfLifeA()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class parentClassB
public:
virtual Life* getLife()=0;
void printMeaningOfLifeB()
Life* A = this->getLife();
std::cout << A->meaning << std::endl;


;

class childClass : public parentClassA, public parentClassB
Life* A;
public:
childClass()
A = new Life;

Life* getLife()
return this->A;

;

int main()

childClass c;
c.printMeaningOfLifeA();
c.printMeaningOfLifeB();

return 0;



The good



  • Does not create copies of the object

  • Accessing the object requires only one function

The bad



  • N/A

Solution 2



Passing an object pointer by reference to a function that requires it.



#include <iostream>

class Life
public:
int meaning;
Life()
meaning = 42;

;

class parentClassA
public:
void printMeaningOfLife(Life*& A)
std::cout << A->meaning << std::endl;
A->meaning = 9001;


;

class childClass : public parentClassA
public:
Life* A;
childClass()
A = new Life;

;

int main()

childClass c;
c.printMeaningOfLife(c.A);
std::cout << c.A->meaning << std::endl;
return 0;



The good



  • Does not create copies of the object

The bad



  • Requires that the pointer be passed on every method between the child class and the accessing location

My conclusion



In my opinion and for my requirements I prefer the first solution. Reason being that I have 5 methods between object and accessing location which would make code readability and maintainability harder. Since the object is only referenced and not copied there should be minimal impact on memory.



The questions



  • Did I make any mistakes?

  • Can this be improved?

  • I could only think of these two solutions. Is there a different/better one?

  • What are other good and bad sides of using these two solutions?

  • Is having a requirement to access a child variable in a parent class a bad design all in itself?

Edit



Since there were no answers I'm assuming that the code and my conclusion is valid. I have also edited the code to use pure virtual functions instead of regular ones to force implementation.









share|improve this question












share|improve this question




share|improve this question








edited Jan 6 at 7:30
























asked Jan 5 at 21:27









CodeBreaker

113




113











  • Is this actual code used in a project, or is it merely hypothetical?
    – Sam Onela
    Jan 5 at 22:08










  • This is the code that I have written to solve the problem that I currently face. If there are no problems with the proposed solutions to the requirements I will implement this into my current project.
    – CodeBreaker
    Jan 5 at 22:21










  • Okay - What task does this code accomplish? Please tell us, and also make that the title of the question via edit. "State what your code does in your title, not your main concerns about it.". Please read How to Ask.
    – Sam Onela
    Jan 5 at 22:23










  • @SamOnela Edits have been made. Unfortunately, my company prevents me to talk and publish code of developing projects so I can't tell what the actual project is and what this solves in it.
    – CodeBreaker
    Jan 5 at 23:05
















  • Is this actual code used in a project, or is it merely hypothetical?
    – Sam Onela
    Jan 5 at 22:08










  • This is the code that I have written to solve the problem that I currently face. If there are no problems with the proposed solutions to the requirements I will implement this into my current project.
    – CodeBreaker
    Jan 5 at 22:21










  • Okay - What task does this code accomplish? Please tell us, and also make that the title of the question via edit. "State what your code does in your title, not your main concerns about it.". Please read How to Ask.
    – Sam Onela
    Jan 5 at 22:23










  • @SamOnela Edits have been made. Unfortunately, my company prevents me to talk and publish code of developing projects so I can't tell what the actual project is and what this solves in it.
    – CodeBreaker
    Jan 5 at 23:05















Is this actual code used in a project, or is it merely hypothetical?
– Sam Onela
Jan 5 at 22:08




Is this actual code used in a project, or is it merely hypothetical?
– Sam Onela
Jan 5 at 22:08












This is the code that I have written to solve the problem that I currently face. If there are no problems with the proposed solutions to the requirements I will implement this into my current project.
– CodeBreaker
Jan 5 at 22:21




This is the code that I have written to solve the problem that I currently face. If there are no problems with the proposed solutions to the requirements I will implement this into my current project.
– CodeBreaker
Jan 5 at 22:21












Okay - What task does this code accomplish? Please tell us, and also make that the title of the question via edit. "State what your code does in your title, not your main concerns about it.". Please read How to Ask.
– Sam Onela
Jan 5 at 22:23




Okay - What task does this code accomplish? Please tell us, and also make that the title of the question via edit. "State what your code does in your title, not your main concerns about it.". Please read How to Ask.
– Sam Onela
Jan 5 at 22:23












@SamOnela Edits have been made. Unfortunately, my company prevents me to talk and publish code of developing projects so I can't tell what the actual project is and what this solves in it.
– CodeBreaker
Jan 5 at 23:05




@SamOnela Edits have been made. Unfortunately, my company prevents me to talk and publish code of developing projects so I can't tell what the actual project is and what this solves in it.
– CodeBreaker
Jan 5 at 23:05















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184399%2faccessing-child-class-variables-from-its-parent-class%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184399%2faccessing-child-class-variables-from-its-parent-class%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation