P
Patrick
I want to achieve the following behaviour but I am having trouble
getting there...bare with me im knew to c++ , so its probably rather
trivial!
To have a class ClassA, and composed within this class is an instance
of another class, ClassB.
I want to have a method getClassB that returns a reference to the
instance of ClassB composed within ClassA.
So when a client calls getClassB they are supplied with a reference to
the instance of ClassB composed within ClassA, so that any
modifications that the client makes to this reference are mirrored in
the instance composed within ClassA.
So far I have constructed ClassA, ClassB, and managed to have an
instance of ClassB composed within ClassA.
My problem is with the "getClassB" method. I have only managed to be
able to return a pointer to the instance of ClassB composed within
ClassA. I want to be able to return a reference (&) not a pointer (*).
The instance of ClassB composed within ClassA is initialised in the
constuctor using
classB = new ClassB(9);
so I have to store it within the class as a pointer i.e.
class ClassA
{ public:
ClassA();
~ClassA();
ClassB* getClassB();
private:
ClassB *classB;
};
then when i go to implement the getClassB method I cannot figure out
how to return a reference.
Is there a way of returning a reference givin that I store classB as a
pointer?
Or do I need to store classB differently? I have tried derefenceing
the pointer return by the "classB = new ClassB(9);" and storing it
explicitly but this didnt seam to work.
The full listing of the code is below with a test file, I want to
maintain the same behaviour as is present at the moment, but using
references instead of pointers in the getClassB method.
That is i want to replace
ClassB* ClassA::getClassB()
{ return classB;
}
with
ClassB& ClassA::getClassB()
{ return classB;
}
any help/pointers greatly appreciated.
pat
----------------------------------------------------------
//Main.cpp
#include <iostream>
#include <cstdlib>
#include "ClassA.h"
#include "ClassB.h"
using namespace std;
int main(int argc, char *argv[])
{
ClassA a;
ClassB *b = a.getClassB();
cout << "a.getClassB() = " << *(a.getClassB()) << "\n";
cout << "b = " << *b << "\n";
b->setValue(0);
cout << "\n\nAfter : b.setValue(0)\n";
cout << "a.getClassB() = " << *(a.getClassB()) << "\n";
cout << "b = " << *b << "\n";
a.getClassB()->setValue(4);
cout << "\n\nAfter : a.getClassB().setValue(4)\n";
cout << "a.getClassB() = " << *(a.getClassB()) << "\n";
cout << "b = " << *b << "\n";
system("Pause");
return 0;
}
----------------------------------------------------------
//ClassA.h
#ifndef CLASSA_H
#define CLASSA_H
#include "ClassB.h"
using namespace std;
class ClassA
{ public:
ClassA();
~ClassA();
ClassB* getClassB();
private:
ClassB *classB;
};
#endif
----------------------------------------------------------
//ClassA.cpp
using namespace std;
#include "ClassA.h"
#include "ClassB.h"
ClassA::ClassA()
{ classB = new ClassB(9);
}
ClassA::~ClassA()
{ delete classB;
}
ClassB* ClassA::getClassB()
{ return classB;
}
----------------------------------------------------------
//ClassB.h
#ifndef CLASSB_H
#define CLASSB_H
#include <iostream>
using namespace std;
class ClassB
{ friend ostream& operator<<(ostream& , const ClassB&);
public:
ClassB(int value);
void setValue(int value);
private:
int value;
};
#endif
----------------------------------------------------------
//ClassB.cpp
#include "ClassB.h"
ClassB::ClassB(int value)
{ (*this).value = value;
}
void ClassB::setValue(int value)
{ (*this).value = value;
}
ostream& operator<<(ostream& output, const ClassB& object)
{ output << object.value;
return output;
}
----------------------------------------------------------
getting there...bare with me im knew to c++ , so its probably rather
trivial!
To have a class ClassA, and composed within this class is an instance
of another class, ClassB.
I want to have a method getClassB that returns a reference to the
instance of ClassB composed within ClassA.
So when a client calls getClassB they are supplied with a reference to
the instance of ClassB composed within ClassA, so that any
modifications that the client makes to this reference are mirrored in
the instance composed within ClassA.
So far I have constructed ClassA, ClassB, and managed to have an
instance of ClassB composed within ClassA.
My problem is with the "getClassB" method. I have only managed to be
able to return a pointer to the instance of ClassB composed within
ClassA. I want to be able to return a reference (&) not a pointer (*).
The instance of ClassB composed within ClassA is initialised in the
constuctor using
classB = new ClassB(9);
so I have to store it within the class as a pointer i.e.
class ClassA
{ public:
ClassA();
~ClassA();
ClassB* getClassB();
private:
ClassB *classB;
};
then when i go to implement the getClassB method I cannot figure out
how to return a reference.
Is there a way of returning a reference givin that I store classB as a
pointer?
Or do I need to store classB differently? I have tried derefenceing
the pointer return by the "classB = new ClassB(9);" and storing it
explicitly but this didnt seam to work.
The full listing of the code is below with a test file, I want to
maintain the same behaviour as is present at the moment, but using
references instead of pointers in the getClassB method.
That is i want to replace
ClassB* ClassA::getClassB()
{ return classB;
}
with
ClassB& ClassA::getClassB()
{ return classB;
}
any help/pointers greatly appreciated.
pat
----------------------------------------------------------
//Main.cpp
#include <iostream>
#include <cstdlib>
#include "ClassA.h"
#include "ClassB.h"
using namespace std;
int main(int argc, char *argv[])
{
ClassA a;
ClassB *b = a.getClassB();
cout << "a.getClassB() = " << *(a.getClassB()) << "\n";
cout << "b = " << *b << "\n";
b->setValue(0);
cout << "\n\nAfter : b.setValue(0)\n";
cout << "a.getClassB() = " << *(a.getClassB()) << "\n";
cout << "b = " << *b << "\n";
a.getClassB()->setValue(4);
cout << "\n\nAfter : a.getClassB().setValue(4)\n";
cout << "a.getClassB() = " << *(a.getClassB()) << "\n";
cout << "b = " << *b << "\n";
system("Pause");
return 0;
}
----------------------------------------------------------
//ClassA.h
#ifndef CLASSA_H
#define CLASSA_H
#include "ClassB.h"
using namespace std;
class ClassA
{ public:
ClassA();
~ClassA();
ClassB* getClassB();
private:
ClassB *classB;
};
#endif
----------------------------------------------------------
//ClassA.cpp
using namespace std;
#include "ClassA.h"
#include "ClassB.h"
ClassA::ClassA()
{ classB = new ClassB(9);
}
ClassA::~ClassA()
{ delete classB;
}
ClassB* ClassA::getClassB()
{ return classB;
}
----------------------------------------------------------
//ClassB.h
#ifndef CLASSB_H
#define CLASSB_H
#include <iostream>
using namespace std;
class ClassB
{ friend ostream& operator<<(ostream& , const ClassB&);
public:
ClassB(int value);
void setValue(int value);
private:
int value;
};
#endif
----------------------------------------------------------
//ClassB.cpp
#include "ClassB.h"
ClassB::ClassB(int value)
{ (*this).value = value;
}
void ClassB::setValue(int value)
{ (*this).value = value;
}
ostream& operator<<(ostream& output, const ClassB& object)
{ output << object.value;
return output;
}
----------------------------------------------------------