C
Connell Gauld
Hi,
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let's call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:
class ClassA
{
public:
// ...
ClassB myVar;
// ...
}
I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and works:
ClassA instance1;
int main()
{
//...
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}
The following, however, compiles but causes an Access Violation when run:
int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);
}
Is there something wrong with my code here? Is the problem in doFuntion?
Any help would be greatly appreciated. Sorry for the horrible
function/class names. Using VC++
Thanks
Connell
I have come across a problem with a slightly complicated section to a
program I am writing.
I have a class (let's call it ClassA) which has, in its public area, a
variable (which happens to be another class, ClassB) like this:
class ClassA
{
public:
// ...
ClassB myVar;
// ...
}
I have a function (doFunction) in yet another class (ClassC) which takes
a pointer to a ClassB as a parameter. Now, the following compiles and works:
ClassA instance1;
int main()
{
//...
ClassC instance2;
instance2.doFunction(&instance1.myVar);
//...
}
The following, however, compiles but causes an Access Violation when run:
int main()
{
ClassA *ptrInstance1;
ptrInstance1=new ClassA;
ClassC instance2;
instance2.doFunction(&ptrInstance1->myVar);
}
Is there something wrong with my code here? Is the problem in doFuntion?
Any help would be greatly appreciated. Sorry for the horrible
function/class names. Using VC++
Thanks
Connell