473,466 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Constructor Behavior

Hi.

I am learning Design Patterns so I pulled this piece of code off the
ng (decorator) . Now I am going through it with the debugger and
seeing what does what and I have found something I just don't
understand.

The constructor for CCoffeeDecorator has this in the list
CCoffeeComponent().
> CCoffeeDecorator( CCoffeeComponent* pComponent = 0) : CCoffeeComponent(), m_pComponent( pComponent ) {};
I see it hitting the CCoffeeComponent() constructor ... but I can't
figure out what this accomplishes. The only thing I can think of, is
that this sets the pointer type to CCoffeeComponent ... but I don't
understand the mechanics are behind it.

Randy

#include <iostream>
#include <string>

using namespace std;

/*
************************************************** ************************************************** ***
abstract base class ** normal - any class can be decorated.
hide constructor prevent construction of a plain component
*
************************************************** ************************************************** ****/
class CCoffeeComponent {
public:
virtual string Info() = 0;

protected:
CCoffeeComponent () {};
string m_Info;
};

/*
************************************************** ************************************************** ***
Furthermore we need a decorator class that "is-a" component and
stores a
pointer to a passed component object:
NOTE: the default NULL-pointer that is used as an end-marker of
the component chain
*
************************************************** ************************************************** ****/
class CCoffeeDecorator : public CCoffeeComponent {
public:
CCoffeeDecorator( CCoffeeComponent* pComponent = 0) :
CCoffeeComponent(), m_pComponent( pComponent ) {};

public:
virtual string Info() {
if( !m_pComponent )
return string("");

return m_pComponent->Info();
}; // delegate info call to actual
implementation

protected:
CCoffeeComponent* m_pComponent;

};

//This base implementation of the decorator delegates any Info() calls
to its
//wrapped component if there is any. In principle we've got the tools
to
//create different component combinations at our fingertips. Now we
get down
//the some sample component implementations:

// create different decorator implementations
class CEspresso: public CCoffeeDecorator
{
public:
CEspresso( CCoffeeComponent* pComponent = 0) :
CCoffeeDecorator(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorator::Info() + string("
espresso"); };
};

// create different decorator implementations
class CSteamedMilk: public CCoffeeDecorator
{
public:
CSteamedMilk( CCoffeeComponent* pComponent = 0) :
CCoffeeDecorator(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorator::Info() + string(" steamed
milk"); };
};

// create different decorator implementations
class CFoamedMilk: public CCoffeeDecorator
{
public:
CFoamedMilk( CCoffeeComponent* pComponent = 0) :
CCoffeeDecorator(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorator::Info() + string(" foamed
milk"); };
};

class CSugar: public CCoffeeDecorator
{
public:
CSugar( CCoffeeComponent* pComponent = 0) :
CCoffeeDecorator( pComponent ){};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorator::Info() + string("
sugar"); };
};

class CMug: public CCoffeeDecorator
{
public:
CMug( CCoffeeComponent* pComponent = 0) :
CCoffeeDecorator( pComponent ){};
// extend info implementation and delegate to base class
string Info() {
return CCoffeeDecorator::Info() + string(" mug");
};
};

int main()
{
/*
The pointer type is component, however the object type is
Decorator. This is the
jist of virtual/polymorphism.

The recursion here is because the constructor for the decorator is
*/
CCoffeeComponent* pCappucino= new CEspresso( new CSugar( new
CFoamedMilk( new CMug) ) );
CCoffeeComponent* pMocca = new CEspresso( new CSugar( new CMug ) );
CCoffeeComponent* pEmpty = new CMug;

cout << "Cappucino components: ";
cout << pCappucino->Info() << endl;
cout << "Mocca components: ";
cout << pMocca->Info() << endl;
cout << "Empty components: ";
cout << pEmpty->Info() << endl;

delete pCappucino;
delete pMocca;

system("PAUSE");
return EXIT_SUCCESS;

return 0;

}

Sep 12 '07 #1
5 1279

I don't know how I managed to butcher the English language so bad ...
Here is what I am actually asking ...

In the CCoffeeDecorator constructor list, there is a call to the
parent class constructor, CCoffeeComponent(). I can't figure out what
this accomplishes, in context of this Decorator example.
Sep 12 '07 #2
Randy wrote:
I am learning Design Patterns so I pulled this piece of code off the
ng (decorator) . Now I am going through it with the debugger and
seeing what does what and I have found something I just don't
understand.

The constructor for CCoffeeDecorator has this in the list
CCoffeeComponent().
>> CCoffeeDecorator( CCoffeeComponent* pComponent = 0) :
CCoffeeComponent(), m_pComponent( pComponent ) {};

I see it hitting the CCoffeeComponent() constructor ... but I can't
figure out what this accomplishes. The only thing I can think of, is
that this sets the pointer type to CCoffeeComponent ... but I don't
understand the mechanics are behind it.
Not sure what your confusion is. CCoffeeDecorator inherits from
CCoffeeComponent. The first item in the constructor initialiser list
is the base class initialiser. It does not have to be there since
CCoffeeComponent is a class that can be default-initialised without
being explicitly mentioned in the initialiser list.

In reality it accomplishes nothing. You can omit it and the base
class subobject will still be constructed.
>
Randy

#include <iostream>
#include <string>

using namespace std;

/*
************************************************** ************************************************** ***
abstract base class ** normal - any class can be decorated.
hide constructor prevent construction of a plain component
*
************************************************** ************************************************** ****/
class CCoffeeComponent {
public:
virtual string Info() = 0;

protected:
CCoffeeComponent () {};
string m_Info;
};

/*
************************************************** ************************************************** ***
Furthermore we need a decorator class that "is-a" component and
stores a
pointer to a passed component object:
NOTE: the default NULL-pointer that is used as an end-marker of
the component chain
*
************************************************** ************************************************** ****/
class CCoffeeDecorator : public CCoffeeComponent {
public:
CCoffeeDecorator( CCoffeeComponent* pComponent = 0) :
CCoffeeComponent(), m_pComponent( pComponent ) {};

public:
virtual string Info() {
if( !m_pComponent )
return string("");

return m_pComponent->Info();
}; // delegate info call to actual
implementation

protected:
CCoffeeComponent* m_pComponent;

};
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '07 #3

Thank you Victor

That was exactly my confusion. As I am new, I just assume that I am
missing a finer point.

Randy
Sep 12 '07 #4
Randy wrote:
That was exactly my confusion. As I am new, I just assume that I am
missing a finer point.
The finer point here *may* be that "everything should be initialised
or something bad's gonna happen" rule is a good rule to live by. But
of course the author would know better, and if they didn't explain
(there or elsewhere in the book), you and I are left to guess...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '07 #5
On Sep 12, 4:34 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Randy wrote:
That was exactly my confusion. As I am new, I just assume that I am
missing a finer point.
The finer point here *may* be that "everything should be initialised
or something bad's gonna happen" rule is a good rule to live by. But
of course the author would know better, and if they didn't explain
(there or elsewhere in the book), you and I are left to guess...
Let's not forget that the author is providing this code as an
example. In practice, the base class might not have a default
constructor (or the default constructor might not be the one we
want), so his example includes a call to the base class
constructor. (But I'm not too sure about this. In the
decorator pattern, the base class will almost surely be an
interface, whose only constructors are the compiler generated
default and copy constructors.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 12 '07 #6

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

Similar topics

9
by: Xiangliang Meng | last post by:
The fragment code: #include <iostream> using namespace std; class Integer { int i; public: Integer() : i(0) {};
10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
0
by: Queen | last post by:
Hi! First of all, perdon for my English. I don't express the thinks very good. Well, I'm working with eclipse 3.1.1, wxWidgets 2.4.2, MSYS 1.0.10 and Mingw 3.1.0. I'm doing a project in c++....
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
11
by: Rimpinths | last post by:
I'm new at developing user controls in C#, and one thing I've noticed right off the bat is that the constructor gets called twice -- once at design time, once at run time. In short, I'm trying...
4
by: neo | last post by:
I made a console based application in vs2k5. I made a class with the name "A" which has one function with the name of "function" and has one member integer variable initialized with 99; its code is...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
10
by: abhash | last post by:
I am bit puzzled at the following piece of code I tried: ---------------------------------------------------------------------------------- #include <iostream> using namespace std; class Test...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.