What do you recommend among the following two designs, considering
all the standard design aspects ?
1)
class base
{
public:
virtual ~base() { }
virtual long int getId() = 0;
private:
static const long int BASE_ID = 10000 ;
};
long int base::getId()
{
return BASE_ID ;
}
class derived11 : public base
{
public:
virtual ~derived11()
{
}
virtual long int getId()
{
return base::getId() + ID ;
}
private:
static const long int ID = 11 ;
};
class derived12 : public base
{
public:
virtual ~derived12()
{
}
virtual long int getId()
{
return base::getId() + ID ;
}
private:
static const long int ID = 12 ;
};
2)
class base
{
public:
virtual long int getId() = 0;
protected:
static const long int BASE_ID = 10000 ;
};
class derived11 : public base
{
public:
virtual long int getId()
{
return BASE_ID + ID ;
}
private:
static const long int ID = 11 ;
};
class derived12 : public base
{
public:
virtual long int getId()
{
return BASE_ID + ID ;
}
private:
static const long int ID = 12 ;
}; 7 1491
qazmlp wrote: What do you recommend among the following two designs, considering all the standard design aspects ?
1) class base { public: virtual ~base() { } virtual long int getId() = 0; private: static const long int BASE_ID = 10000 ; };
long int base::getId() { return BASE_ID ; }
You have coupled the concept of the base id with the concept of all the
derived id overrides. They are different. (To put this into "duplication"
terms, all the derived getId() implementations duplicate each other.) Try
this:
class base
{
public:
virtual ~base() { }
long int getId() { return BASE_ID + getIdOffset(); }
private:
virtual long getIdOffset() = 0;
static const long int BASE_ID = 10000 ;
};
long base::getIdOffset()
{
return 0;
}
long derived11::getIdOffset()
{
return 11;
}
long derived12::getIdOffset()
{
return 12;
}
That design exposes less virtual methods in base's public interface.
--
Phlip http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
The first is complicated and slow. The second is simple and fast. I
don't see any benefit to the polymorphism used in the first. qa********@rediffmail.com (qazmlp) might (or might not) have written
this on (or about) 31 Jan 2004 03:36:15 -0800, : What do you recommend among the following two designs, considering all the standard design aspects ?
1) class base { public: virtual ~base() { } virtual long int getId() = 0; private: static const long int BASE_ID = 10000 ; };
long int base::getId() { return BASE_ID ; }
class derived11 : public base { public: virtual ~derived11() { } virtual long int getId() { return base::getId() + ID ; }
private: static const long int ID = 11 ; };
class derived12 : public base { public: virtual ~derived12() { } virtual long int getId() { return base::getId() + ID ; }
private: static const long int ID = 12 ; };
2) class base { public: virtual long int getId() = 0; protected: static const long int BASE_ID = 10000 ; };
class derived11 : public base { public: virtual long int getId() { return BASE_ID + ID ; }
private: static const long int ID = 11 ; };
class derived12 : public base { public: virtual long int getId() { return BASE_ID + ID ; }
private: static const long int ID = 12 ; };
Robert C. Martin | "Uncle Bob"
Object Mentor Inc. | unclebob @ objectmentor . com
501 N. Riverside Dr.| Tel: (800) 338-6716
Suite 206 | Fax: (847) 775-8174 | www.objectmentor.com
| | www.XProgramming.com
Gurnee, IL, | Training and Mentoring | www.junit.org
60031 | OO, XP, Agile, C++, Java, C# | http://fitnesse.org qa********@rediffmail.com (qazmlp) wrote in message news:<db**************************@posting.google. com>... What do you recommend among the following two designs, considering all the standard design aspects ?
.... virtual long int getId() = 0;
....
Your getId function is returning a different value for each type,
and the same value for all instances of the same type. In other
words, it is effectively a type identifier.
I have two questions, both concerning what the clients use
this facility for..
1) Won't the clients work just as well by asking an object for
its type? Isomorphic information will be returned.
2) Are the clients treating a variable in different ways
according to its returned getId() value? If so, please consider
using polymorphism instead! u.*************@objectmentor.com (Uncle Bob (Robert C. Martin)) wrote
(abridged): The first is complicated and slow. The second is simple and fast.
If the first base::getId() function is inlined, they could be the same
speed. Indeed, they could generate the same assembler.
I don't see any benefit to the polymorphism used in the first.
It's easier to make changes to the first. The second base class exposes
more of its representation to the derived classes. I don't know if that
matters in the example.
-- Dave Harris, Nottingham, UK
On Tue, 3 Feb 2004 19:40 +0000 (GMT Standard Time), br******@cix.co.uk
(Dave Harris) wrote: u.*************@objectmentor.com (Uncle Bob (Robert C. Martin)) wrote (abridged): The first is complicated and slow. The second is simple and fast.
If the first base::getId() function is inlined, they could be the same speed. Indeed, they could generate the same assembler.
The first base::getId() is a pure virtual function. I didn't think
you could inline that. un******@objectmentor.com (Robert C. Martin) wrote (abridged): If the first base::getId() function is inlined, they could be the same speed. Indeed, they could generate the same assembler.
The first base::getId() is a pure virtual function. I didn't think you could inline that.
You can. If it is called with the base::getId() syntax, there's no virtual
dispatch involved, so the compiler knows at compile-time exactly which
function body will be invoked, and if it knows the source it can inline
it.
-- Dave Harris, Nottingham, UK
On Wed, 4 Feb 2004 22:32 +0000 (GMT Standard Time), br******@cix.co.uk
(Dave Harris) wrote: un******@objectmentor.com (Robert C. Martin) wrote (abridged): >If the first base::getId() function is inlined, they could be the same >speed. Indeed, they could generate the same assembler.
The first base::getId() is a pure virtual function. I didn't think you could inline that.
You can. If it is called with the base::getId() syntax, there's no virtual dispatch involved, so the compiler knows at compile-time exactly which function body will be invoked, and if it knows the source it can inline it.
<duh>
Doh!
</duh> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Roger Leigh |
last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++
for Linux in 21 Days--I know there are better) states that "static
member functions cannot access any non-static member...
|
by: trying_to_learn |
last post by:
while seeing an example i was surprised to see that a const member
function is allowed to change a static data member as shown below. I am
trying to reason why.... and my guess is that static data...
|
by: ma740988 |
last post by:
Select parameters in a vendors API file is as follows:
#ifdef __cplusplus
typedef void (*VOIDFUNCPTR)(...); /* ptr to function returning void
*/
#else
typedef void (*VOIDFUNCPTR)(); ...
|
by: Martin Vorbrodt |
last post by:
I'm designing a Matrix class of 4x4 matrices used in computer graphics. Both
OpenGL and Direct3D can take a pointer to array of 16 floats which represent
the values in the matrix. OGL takes it in...
|
by: Grizlyk |
last post by:
Somebody have offered std colors to C++ in the msg here:
http://groups.google.com/group/comp.lang.c++/browse_frm/thread/2e5bb3d36ece543b/1acf6cd7e3ebdbcd#1acf6cd7e3ebdbcd
The main objection to...
|
by: nw |
last post by:
Hi,
I was wondering if someone would be able to give me some comments on
the following class structure, it feels to me as if there must be a
better way, but I'm unsure what it is, perhaps I...
|
by: mimi |
last post by:
1.Instead of using macro, I would like to use static const variables
in such situations.
class Foo {
public:
static const int SOMEVALUEWITHFOO = 1;
}
int main()
{
|
by: tomPee |
last post by:
Hi,
I have the following problem:
I am trying to make some sort of base class menu that i can then use to derive other menu's from. Those menu's should then be able to interact with each other....
|
by: Soumen |
last post by:
For one of my project, I want to use auto_ptr. But I'm facing few
issues.
1. Say I've Controller class. I don't want to allow user to create
object of this class unless all arguments
are valid....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |