recommended design for 'static const' member access

Q

qazmlp

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 ;
};
 
P

Phlip

qazmlp said:
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.
 
U

Uncle Bob (Robert C. Martin)

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.


(e-mail address removed) (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
 
G

Graham Perkins

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!
 
D

Dave Harris

(e-mail address removed) (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
 
R

Robert C. Martin

(e-mail address removed) (Uncle Bob (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.
 
D

Dave Harris

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
 
R

Robert C. Martin

(e-mail address removed) (Robert C. Martin) wrote (abridged):

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top