473,241 Members | 1,704 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,241 software developers and data experts.

Passing reference to derived class object to base class constructor

The derived class below passes a reference to an object in its own
class to its base calss constructor. The code compiles and will run
successfully as long as the base class constructor does not attempt to
access the object -- since m_object is not actually created and
initizialized until after the base constructor has been called.

Any thoughts on the practice below?
class Base
{
public:
Base(Object& ref) {};
~Base() {};
};

class Derived : public Base
{
public:
Derived(int param) : Base(m_object), m_object(param) {};

protected:
Object m_object;
};

Mar 7 '06 #1
4 5209
Jeff wrote:
The derived class below passes a reference to an object in its own
class to its base calss constructor. The code compiles and will run
successfully as long as the base class constructor does not attempt to
access the object -- since m_object is not actually created and
initizialized until after the base constructor has been called.

Any thoughts on the practice below?
Bad practice. Whoever comes later to do something with 'Base' is not
aware that 'ref' cannot be used during construction. There are some
situations (like if 'Base' as a member of type 'Object&' that cannot
be set later, and has to be initialised) in which you have to pass the
reference to the c-tor, but those cases need to be _extensively_
documented in the code.

On the second thought, it's possible that by redesigning one could work
around this... Need to be dealt with on the case-by-case basis.
class Base
{
public:
Base(Object& ref) {};
~Base() {};
The semicolons after function bodies are unnecessary. And they really
make the code look unclean.
};

class Derived : public Base
{
public:
Derived(int param) : Base(m_object), m_object(param) {};

protected:
Object m_object;
};


V
--
Please remove capital As from my address when replying by mail
Mar 7 '06 #2

Jeff wrote:
The derived class below passes a reference to an object in its own
class to its base calss constructor. The code compiles and will run
successfully as long as the base class constructor does not attempt to
access the object -- since m_object is not actually created and
initizialized until after the base constructor has been called.

Any thoughts on the practice below?
class Base
{
public:
Base(Object& ref) {};
~Base() {};
};

class Derived : public Base
{
public:
Derived(int param) : Base(m_object), m_object(param) {};

protected:
Object m_object;
};


The standard requires base class initialized before derived class
member object initialization. Therefore your code will crash if base
constructor access m_object because m_object is not initialized yet.

Mar 7 '06 #3
Fei Liu wrote:
Jeff wrote:
The derived class below passes a reference to an object in its own
class to its base calss constructor. The code compiles and will run
successfully as long as the base class constructor does not attempt to ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^access the object -- since m_object is not actually created and ^^^^^^^^^^^^^^^^^initizialized until after the base constructor has been called.

Any thoughts on the practice below?
[...] The standard requires base class initialized before derived class
member object initialization. Therefore your code will crash if base
constructor access m_object because m_object is not initialized yet.


So, if A precedes B, then B must be following A. Did I get it right?
Mar 7 '06 #4

"Jeff" <mu*******@gmail.com> wrote in message
news:11*********************@v46g2000cwv.googlegro ups.com...
| The derived class below passes a reference to an object in its own
| class to its base calss constructor. The code compiles and will run
| successfully as long as the base class constructor does not attempt to
| access the object -- since m_object is not actually created and
| initizialized until after the base constructor has been called.
|
| Any thoughts on the practice below?
| class Base
| {
| public:
| Base(Object& ref) {};
| ~Base() {};
| };
|
| class Derived : public Base
| {
| public:
| Derived(int param) : Base(m_object), m_object(param) {};
|
| protected:
| Object m_object;
| };
|

If the base class needs a reference to a derived class's component, then
thats a clear sign that that component should be in the base class instead.

class Object { };

class Base
{
Object m_object;
public:
Base(Object o) : m_object(o) { }
virtual ~Base() = 0 { }
virtual Object getObject() const = 0 { return m_object; }
};

class Derived : public Base
{
public:
Derived(Object o) : Base(o) { }
~Derived() { }
/* required pure-virtual interface */
Object getObject() const { return Base::getObject(); }
};

int main()
{
Object obj;
// Base b(obj); cannot instantiate abstract class
Derived d(obj);

return 0;
}
____
hmm, good candidate(s) for a template.

template< class T >
class Base
{
T m_t;
public:
Base(T t) : m_t(t) { }
virtual ~Base() = 0 { }
virtual T getT() const = 0 { return m_t; }
};

template< class T >
class Derived : public Base< T >
{
public:
Derived(T t) : Base(t) { }
~Derived() { }
/* required pure-virtual interface */
T getT() const { return Base::getT(); }
};

class Object { };
class Object2 { };

int main()
{
Object obj;
Derived< Object > d(obj);

Object2 obj2;
Derived< Object2 > d2(obj2);

return 0;
}
Mar 8 '06 #5

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

Similar topics

12
by: Tim Clacy | last post by:
Your expertise will be appreciated... Here's a general templatised class; all specialisations of this class should have a pointer to a specialisation of the same, templatised type: ...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
1
by: user | last post by:
Hi, If I assign in the base class' constructor: myMember = 5; and assign in the derived class' constructor: myMember = 10; where myMember was derived from the base class,
4
by: Nataraj M | last post by:
Hi, I just don't want anybody derive a class from my class. For example: /////////////////////// //MY CODE class MyClass { .... }; ///////////////////////
2
by: Zac | last post by:
Alright anyone who has 2c throw it in... I am working through a custom xml serializer and have come upon a conundrum, given our class design. The interface implemented on the base class (base...
8
by: TS | last post by:
I am trying to get set a property of a control on the inherited class from base class. I imagine i have to use reflection, so could someone give me the code to do it? something like this?...
0
by: dkode | last post by:
Hello, I know this can be done, but it's beyond me at the moment, Here is my inheritance chain: class NavRole : UserControl { protected void AddSubmenu(string submenuFile); } class artist...
9
by: phl | last post by:
Hi, I have a class, which inherits a class that inherits a List class. How do I pass this base List Class to another function by reference? At the moment I just create a new List for doing this but...
15
by: Anthony Greene | last post by:
This is probably a very introductory object-oriented question, but it has been nagging me for years, and since I've never been able to find the right answer, I've had to work around it with...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.