473,396 Members | 2,092 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,396 software developers and data experts.

How to build a better diamond?

Below is some code I wrote to get a better understanding of the dynamic
verses static type resolution. My intention was to see if there was a way
to use references for the static access to the subobjects of an object of
derived type. I thought I had seen a way to use references to statically
access member functions in a way syntactically similar to how pointers
dynamically access the same objects member functions.

The overall objective is to 'navigate' the diamond in the most elegant and
safe way. I had hoped to use this as a follow up to the discussion on
references and pointers and other objects(sic). Unfortunately I wasn't able
to find a way to do what I thought I could.

Do othere people have comments on how best to 'navigate' the class
hierarchy?

#include <string>
#include<iostream>

using std::string;
class North
{
public:
North(const string& name="North"):m_name(name)
{}
virtual string& toString() { return m_name; }
private:
string m_name;
};

class East : virtual public North
{
public:
East(const string& name="East"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(North::toString());
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};

class West : virtual public North
{
public:
West(const string& name="West"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(North::toString());
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};

class South : public East, public West
{
public:
South(const string& name="South"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(East::toString());
*ret += "::";
*ret += West::toString();
*ret += "::";
*ret += m_name;
return *ret;
}
private:
string m_name;
};
int main()
{
using std::cout;
using std::endl;

North n;
East e;
West w;
South s;
cout<<n.toString()<<endl;
cout<<e.toString()<<endl;
cout<<w.toString()<<endl;
cout<<s.toString()<<endl;

North ns = static_cast<North>(s);
East es = static_cast<East>(s);
West ws = static_cast<West>(s);
cout<<ns.toString()<<endl;
cout<<es.toString()<<endl;
cout<<ws.toString()<<endl;
}

/*OUTPUT*/
North
North::East
North::West
North::East::North::West::South
North
North::East
North::West

--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #1
4 1707
* "Steven T. Hatton" <su******@setidava.kushan.aa> schriebt:


#include <string>
#include<iostream>

using std::string;
class North
{
public:
North(const string& name="North"):m_name(name)
{}
virtual string& toString() { return m_name; }
Ungood to return reference to internal member here, and should be const
function to allow calls on const object, thus:
virtual string toString() const { return m_name; }
Also, consider a private or protected function name() instead of member;
the function won't occupy storage per object.

private:
string m_name;
};

class East : virtual public North
{
public:
East(const string& name="East"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(North::toString());
*ret += "::";
*ret += m_name;
return *ret;
}
1) The code does not deallocate 'ret'.
2) It's not exception safe. For exception safe you'd have to use
std::auto_ptr. But
3) A better way is 'return North::toString() + "::" + m_name';

North::East::North::West::South


I suggest forgetting that diamond inheritance pattern for a while;
concentrate on just creating some useful program.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
* "Steven T. Hatton" <su******@setidava.kushan.aa> schriebt:


#include <string>
#include<iostream>

using std::string;
class North
{
public:
North(const string& name="North"):m_name(name)
{}
virtual string& toString() { return m_name; }
Ungood to return reference to internal member here, and should be const
function to allow calls on const object, thus:
virtual string toString() const { return m_name; }
Also, consider a private or protected function name() instead of member;
the function won't occupy storage per object.

private:
string m_name;
};

class East : virtual public North
{
public:
East(const string& name="East"):m_name(name)
{}
virtual string& toString()
{
string* ret = new string(North::toString());
*ret += "::";
*ret += m_name;
return *ret;
}
1) The code does not deallocate 'ret'.
2) It's not exception safe. For exception safe you'd have to use
std::auto_ptr. But
3) A better way is 'return North::toString() + "::" + m_name';

North::East::North::West::South


I suggest forgetting that diamond inheritance pattern for a while;
concentrate on just creating some useful program.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3
Alf P. Steinbach wrote:
* "Steven T. Hatton" <su******@setidava.kushan.aa> schriebt:


#include <string>
virtual string& toString() { return m_name; }
Ungood to return reference to internal member here,


Agreed. I wasn't sure how best to return a copy.
and should be const
function to allow calls on const object, thus:
I wasn't sure of the syntax. I tried a couple combination, but they didn't
compile. I really think I need to get the C++ Pocket Reference. It's hard
to find good paradigms in the materials I have at hand.
virtual string toString() const { return m_name; }
Thanks for the example.
Also, consider a private or protected function name() instead of member;
the function won't occupy storage per object.
I'm not exactly sure what you're suggesting. Is this related to
std::type_info?

1) The code does not deallocate 'ret'.
I was aware of that. This is one of the reasons Java's string is immutable.
It's also why Xerces C++ XMLstring class is such a monster. I guess the way
to address a situation like this is with handles. I didn't want to get
that deep into things just yet.
2) It's not exception safe. For exception safe you'd have to use
std::auto_ptr. But
3) A better way is 'return North::toString() + "::" + m_name';
I wasn't sure what that would do. I /believe/ it does about the same thing
as what I provided, but never creates an explicit pointer. I don't know
what scoping and lifetime rules apply. If it's allocated as dynamic
storage (which I believe it will be), the onus is on me to release it when
I'm finished.
North::East::North::West::South


I suggest forgetting that diamond inheritance pattern for a while;
concentrate on just creating some useful program.


The inheritance doesn't seem all that confusing in general. The thing I'm
really trying to get a handle on through this exercise is how references
and pointers work WRT overloaded (and hidden) virtual functions. I guess
that wasn't very obvious since all the code I posted used values rather
than pointers and references. I figure, if I can keep creating instructive
and interesting examples based on the pattern of the code I posted, it will
serve as a way to reinforce the trickier lessons surrounding inheritance.

As for writing useful programs, that's why I gave up on C++ last time. It
was too frustrating to know what I wanted to do, but not how to do it. I
had created the UI shown below with the data elements backed by
QDomElements. I tried creating another feature using templates and ended
up so tangled up I just quit.

http://baldur.globalsymmetry.com/gs-...s/kgrammar.png

--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #4
Alf P. Steinbach wrote:
* "Steven T. Hatton" <su******@setidava.kushan.aa> schriebt:


#include <string>
virtual string& toString() { return m_name; }
Ungood to return reference to internal member here,


Agreed. I wasn't sure how best to return a copy.
and should be const
function to allow calls on const object, thus:
I wasn't sure of the syntax. I tried a couple combination, but they didn't
compile. I really think I need to get the C++ Pocket Reference. It's hard
to find good paradigms in the materials I have at hand.
virtual string toString() const { return m_name; }
Thanks for the example.
Also, consider a private or protected function name() instead of member;
the function won't occupy storage per object.
I'm not exactly sure what you're suggesting. Is this related to
std::type_info?

1) The code does not deallocate 'ret'.
I was aware of that. This is one of the reasons Java's string is immutable.
It's also why Xerces C++ XMLstring class is such a monster. I guess the way
to address a situation like this is with handles. I didn't want to get
that deep into things just yet.
2) It's not exception safe. For exception safe you'd have to use
std::auto_ptr. But
3) A better way is 'return North::toString() + "::" + m_name';
I wasn't sure what that would do. I /believe/ it does about the same thing
as what I provided, but never creates an explicit pointer. I don't know
what scoping and lifetime rules apply. If it's allocated as dynamic
storage (which I believe it will be), the onus is on me to release it when
I'm finished.
North::East::North::West::South


I suggest forgetting that diamond inheritance pattern for a while;
concentrate on just creating some useful program.


The inheritance doesn't seem all that confusing in general. The thing I'm
really trying to get a handle on through this exercise is how references
and pointers work WRT overloaded (and hidden) virtual functions. I guess
that wasn't very obvious since all the code I posted used values rather
than pointers and references. I figure, if I can keep creating instructive
and interesting examples based on the pattern of the code I posted, it will
serve as a way to reinforce the trickier lessons surrounding inheritance.

As for writing useful programs, that's why I gave up on C++ last time. It
was too frustrating to know what I wanted to do, but not how to do it. I
had created the UI shown below with the data elements backed by
QDomElements. I tried creating another feature using templates and ended
up so tangled up I just quit.

http://baldur.globalsymmetry.com/gs-...s/kgrammar.png

--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #5

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

Similar topics

4
by: Steven T. Hatton | last post by:
Below is some code I wrote to get a better understanding of the dynamic verses static type resolution. My intention was to see if there was a way to use references for the static access to the...
5
by: Tony Johansson | last post by:
Hello Experts! It it correct to say that a solution to the diamond problem is to use virtual inheritance with virtual base classes. //Tony
10
by: coinjo | last post by:
I need to write a program which takes a number as an input and prints a diamond of # and $. The number of rows in the shape is equal to the number entered by the user. Your program should display...
2
by: emma middlebrook | last post by:
Hi I come from a C++ background and so am familiar with the 'dreaded inheritance diamond' i.e. the ambiguity of which data to use when a class appears twice in the hierarchy. Here's a link for...
4
by: Schüle Daniel | last post by:
Hello C++ NG, I encountered a strange behaviour in this code #include <iostream> #include <cstdlib> #include <string> using std::cout; using std::endl;
14
by: jasson118 | last post by:
i am a newber to C++ and have trouble with one of the problem from the book. can anyone able to use nested loops to display a diamond shape with " * ". * *** ***** ******* ********* *******
28
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am developing C++ COM native code (unmanaged C++) using Visual Studio 2005. I do not take any new features of 64-bit platform, and currently my code runs fine on 32-bit...
9
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
6
by: Rocketmagnet | last post by:
Hi all, I have been kind of forced (honestly) into writing a class structure which contains a Diamond of Death, and I'm not entirely sure what to do about it. This is a simplified version of my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.