Hello all,
I am a new programmer and I am trying to look at how two classes can interact if one class is called by another. What I am trying to do is have a control class. This control class will then make a decision in the constructor about what "sub" classes to call (they are not inherited classes so not strictly a sub class). Since I don't know ahead of time which sub classes will be called I use a pointer in my class header file. In the constructor I then make a decision about the sub classes to call, instantiate the sub classes, and set the pointer to that sub class.
Now here is the my problem; once the constructor finishes, the new sub class I made is destroyed. Later in the control class I have a function that uses the sub class pointer. The pointer works fine and all of the data is passed through the pointer and the function. My worry is that I am working off an artifact in the memory. Since the destructor is called on the sub class the pointer should be randomly pointing into space. Am I missing something about constructors/destructors and pointers? I am sure I am missing something, but I want to learn good technique and make sure I have easily modifiable code. Any help or insight is appreciated.
Below is my test code:
main file
Code:
#include "stdafx.h"
using namespace std;
int main()
{
int kb = 0; // just a value to halt the program from closing the window
{ // defining a new scope to test destruction and construction
int val_test = 5; // initialize the value val_test;
Class1 input(0); // call class one and initialize class one's variable
for (int ii = 0; ii < 10; ii++){ // loop to test this setup
cout << "my value: " << ii << endl;
input.set(ii); // resetting the values in class 1
}
}
cin >> kb; // just a halt command
return 0;
}
my control class header and cpp files
Code:
#pragma once
class Class2;
class Class1
{
public:
Class1(int a); // constructor
~Class1(void); // destructor
void set(int a); // set function
int & get(); // get function
friend class Class2; // make class two a friend
private:
int b; // integer variable testing value passing
Class2 *Ptr; // pointer to second class that is called from this class
};
,
Code:
#include "StdAfx.h"
#include "Class1.h"
using namespace std;
Class1::Class1(int a)
{
b = a; // initial assignment
Class2 sub_class; // creating a class2 object
Ptr = &sub_class; // setting a pointer to class2 object
cout << "constructor value of class1: " << b << endl; // output statement to see the value was initialized
}
void Class1::set(int a)
{
b = a; // set the integer value
cout << "Class1 stored value is: " << b << endl; // out put the set interger value
cout << "Calling second sub_class" << endl; // calling the pointer to class2
Ptr->get(this); // pointer to second class passes the pointer of this object
}
int &Class1::get()
{
return b; // return a reference to this value
}
Class1::~Class1(void)
{
cout << "my control class destructor is called" << endl; // tell me when the destructor is called
}
my sub class header and cpp files
Code:
#pragma once
class Class1;
class Class2
{
public:
Class2(void); // constructor
~Class2(void); // destructor
friend class Class1; // friending class 1 to see private data
private:
int val; // value i want passed in from class1 object
int *valPtr; // pointer
Class1 *Ptr; // pointer to class one object
void get(Class1 *cPtr); // get function to get value from class1 object
};
,
Code:
#include "StdAfx.h"
#include "Class2.h"
using namespace std;
Class2::Class2(void)
{
val = 0;// initialize the value
}
void Class2::get(Class1 *cPtr)
{
Ptr = cPtr; // set the pointer to class1 object
val = Ptr->get(); // calling the class1 function get to pass the data to class 2
cout << " Sub_class value is: " << val << endl; // output what the data is
}
Class2::~Class2(void)
{
cout << "my sub_class destructor is called" << endl; // telling me when this destructor is called
}
thanks again and I hope this is readable
Specter