473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Object Composition & Returning References....

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;
}
----------------------------------------------------------
Jul 22 '05 #1
4 1752

"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
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);
Do you have a good reason for doing it this way? Its generally better to
avoid pointers, if you can.
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.


Simple

ClassB& getClassB() { return *classB; }

john
Jul 22 '05 #2
Thanks for the reply

I had implemented getClassB as you suggested, but when i was doing the
following in the code that called it

ClassB b = a.getClassB();

I understand now that the "copy" constructor was being used here,
right?

How can i (or is it possible to) implement the getClassB() method s.t.
when i do the following

ClassB b = a.getClassB();

i am not using the copy constructor, and "b" refers to the instance of
ClassB composed within ClassA, not a copy of it.
Do you have a good reason for doing it this way? Its generally better to
avoid pointers, if you can.


Yes i think so, but could be wrong, this is just a toy example to help
me figure out how to get it working.

My ClassA will store a Vector of (references to) ClassB objects.
ClassA will allow the client add and delete ClassB objects from this
Vector.
And I need to be able to return a reference to this Vector from ClassA
to the client.
So I was trying to figure out how to get it working with just an
instance of ClassB composed within ClassA, then i will hopefully
understand how to get it working with a Vector containing instances of
ClassB. Maybe its just as easy as my example at the moment.

much appreciated.

pat
Jul 22 '05 #3

"Patrick" <go***********@yahoo.co.uk> wrote in message
news:94**************************@posting.google.c om...
Thanks for the reply

I had implemented getClassB as you suggested, but when i was doing the
following in the code that called it

ClassB b = a.getClassB();

I understand now that the "copy" constructor was being used here,
right?

How can i (or is it possible to) implement the getClassB() method s.t.
when i do the following

ClassB b = a.getClassB();

i am not using the copy constructor, and "b" refers to the instance of
ClassB composed within ClassA, not a copy of it.


Use a reference

ClassB& b = a.getClassB();

but remember that you cannot 'reseat' references, i.e. if later you do

b = a2.getClassB();

that will not make b refer to the ClassB object in a2, instead it will copy
the a2 ClassB to the a ClassB (which b will still refer to).

If you think you might need to change where a variable refers to, you are
better of using pointers than references.

john
Jul 22 '05 #4
Just for the record, this is what i was trying to do.
It looks right, it seems to work right, so hopefully its gotta be
right, right?

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 b1(1);
ClassB* b2 = new ClassB(2);
ClassB b3(3);

//adding element to vector composed within instance of ClassA
a.addElement(b1);

//making a reference to vector composed within instance of ClassA
vector<ClassB>& classBVec2 = a.getClassBVec();

//altering an object in the vector composed within instance of ClassA,
//via reference to it
classBVec2[0].setValue(11);

//altering the vector composed within instance of ClassA,
//via reference to it
classBVec2.push_back(*b2);

//altering the vector composed within instance of ClassA,
//via reference to it
classBVec2.push_back(b3);

system("Pause");
return 0;
}
------------------------------------------------------------------------
//ClassA.h

#ifndef CLASSA_H
#define CLASSA_H
#include <vector>

#include "ClassB.h"

using namespace std;

class ClassA
{ public:
ClassA();
void addElement(ClassB& element);
vector<ClassB>& getClassBVec();

private:
vector<ClassB> classBVec;
};

#endif

------------------------------------------------------------------------
//ClassA.cpp

using namespace std;
#include "ClassA.h"

ClassA::ClassA()
{
}

void ClassA::addElement(ClassB& element)
{ classBVec.push_back(element);
}

vector<ClassB>& ClassA::getClassBVec()
{ return classBVec;
}

------------------------------------------------------------------------
//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;
}
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I...
7
by: Pablo J Royo | last post by:
Hello: i have a function that reads a file as an argument and returns a reference to an object that contains some information obtained from the file: FData &ReadFile(string FilePath); But ,...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
6
by: Matthias Kaeppler | last post by:
Hello, during a discussion on a C++ internet forum, some question came up regarding references and the lifetime of the objects they alias. I can't find any clear wording on that in the draft...
1
by: Tony Johansson | last post by:
Hello experts! I know it's easy to override when you have inheritance. But if you instead use object composition having a pointer to a class X. If you want to override when having this object...
4
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
11
by: Xiaoshen Li | last post by:
Dear Sir, I am a little puzzled about a function returning a class object, for example, suppose I hava a class Money and a method: Money lastYear(Money aMoney) { Money tempMoney; ......
2
by: Gary Wessle | last post by:
Hi what is the difference "in code writing" between aggregation and composition? is the following correct? aggregation (life time of both objects are independent). class A{ /* ... */ };...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.