abraxas334
October 4th, 2010, 12:09 PM
I have a question about class structures in c++.
i have a manager class cManager. Within this class a private variable is an instance of an other class cProtein.
roughly looks like this:
#include "cProtein.h"
class cManager {
public:
//this is all that needs to be public
cManager();
cManager(const cMonteCarloManager& orig);
virtual ~cManager();
void setup_run(string, string);
private:
//functions
readConfig assignConfigValues(string);
//variables
cProtein cP;
cProtein tP;
int begN;
int endN;
};
cProtein cP calls the default constructor correct? At the moment cP has a const static int length variable, which I would like to be able to change through my config file. I guess it is not valid to write sokething like:
.
.
//variables
cProtein tP(int length);
if length is only defined after the config file has beed read this is not legal i guess. My question is how do i make the variable length generally available to cProtein and cManager without having a const static int like i do at the moment.
This is cProtein.h/cpp
public:
class cProtein{
public:
cProtein();
cProtein(int);
virtual ~cProtein();
//functions
//variables
private:
//functions
//variables
static const int spaceSize = 256;
static const int length = 48;
int *** space;
};
cProtein::cProtein() {
int b1 = -1;
int b2 = -1;
srand((unsigned int) time(0));
std::cout << "Allocating space...\n";
space = new int**[spaceSize];
std::cout << "great!\n";
for (int i = 0; i < spaceSize; i++)
{
space[i] = new int*[spaceSize];
for (int j = 0; j < spaceSize; j++)
{
space[i][j] = new int[spaceSize];
}
}
std::cout << "Allocated space!\n";
resetSpace() //this function needs a valid length (int)
}
Just to summarize: What I want to be able to do is read my config file which gives me a value for length. And then I want to assign that length value to my cProtein object.
So my solution for this was that I'd have cProtein cP declared in the class but let it call the empty construcotr cProtein::cProtein(){}
and then sometime in my setup_run fuction after reading the config file do something like
int length = 48;
cProtein test(length);
cP = test;
std::cout<< cP.length<<std::endl;
This does not actually assing the length to the now public length variable in my cProtein class.
In the new constructor I write something like this:
cProtein::cProtein(int len)
{
....//some other stuff assinging space etc
len = this->length;
len = length;
//I have tried both ways but cP.length never returns 48.
}
I am lost with this. Any ideas where I am going wrong? I guess pointers would be more appropriate here too?
i have a manager class cManager. Within this class a private variable is an instance of an other class cProtein.
roughly looks like this:
#include "cProtein.h"
class cManager {
public:
//this is all that needs to be public
cManager();
cManager(const cMonteCarloManager& orig);
virtual ~cManager();
void setup_run(string, string);
private:
//functions
readConfig assignConfigValues(string);
//variables
cProtein cP;
cProtein tP;
int begN;
int endN;
};
cProtein cP calls the default constructor correct? At the moment cP has a const static int length variable, which I would like to be able to change through my config file. I guess it is not valid to write sokething like:
.
.
//variables
cProtein tP(int length);
if length is only defined after the config file has beed read this is not legal i guess. My question is how do i make the variable length generally available to cProtein and cManager without having a const static int like i do at the moment.
This is cProtein.h/cpp
public:
class cProtein{
public:
cProtein();
cProtein(int);
virtual ~cProtein();
//functions
//variables
private:
//functions
//variables
static const int spaceSize = 256;
static const int length = 48;
int *** space;
};
cProtein::cProtein() {
int b1 = -1;
int b2 = -1;
srand((unsigned int) time(0));
std::cout << "Allocating space...\n";
space = new int**[spaceSize];
std::cout << "great!\n";
for (int i = 0; i < spaceSize; i++)
{
space[i] = new int*[spaceSize];
for (int j = 0; j < spaceSize; j++)
{
space[i][j] = new int[spaceSize];
}
}
std::cout << "Allocated space!\n";
resetSpace() //this function needs a valid length (int)
}
Just to summarize: What I want to be able to do is read my config file which gives me a value for length. And then I want to assign that length value to my cProtein object.
So my solution for this was that I'd have cProtein cP declared in the class but let it call the empty construcotr cProtein::cProtein(){}
and then sometime in my setup_run fuction after reading the config file do something like
int length = 48;
cProtein test(length);
cP = test;
std::cout<< cP.length<<std::endl;
This does not actually assing the length to the now public length variable in my cProtein class.
In the new constructor I write something like this:
cProtein::cProtein(int len)
{
....//some other stuff assinging space etc
len = this->length;
len = length;
//I have tried both ways but cP.length never returns 48.
}
I am lost with this. Any ideas where I am going wrong? I guess pointers would be more appropriate here too?