By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,465 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,465 IT Pros & Developers. It's quick & easy.

Constants in base classes

P: n/a
Hi All,
I have two classes that have the same functionality, but with
different constants, e.g. database table name.
I've defined a base class that gets the constants in the constructor,
and maintain the functionality. It works fine, but not very
efficiently.
Here's an example:
class base {
public:
base (string &const1, string @const2) {s1 = const1; s2 = const2;}
private:
string s1, s2;
};
class kid1 : public base {
public:
kid1 () {base ("Child 1", "Table 1");}
};
class kid2 : public base {
public:
kid2 () {base ("Child 2", "Table 2");}
};

My problem is that each object has all the constants as variables.
Is there any way to improve the efficiency?
Thanks a lot in advance,
Omer.
Nov 18 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Nov 18, 12:26*pm, Omer <omer...@yahoo.comwrote:
Hi All,
I have two classes that have the same functionality, but with
different constants, e.g. database table name.
I've defined a base class that gets the constants in the constructor,
and maintain the functionality. It works fine, but not very
efficiently.
Here's an example:
class base {
public:
* * base (string &const1, string @const2) {s1 = const1; s2 = const2;}
@ is not a valid member of C++ character set.
private:
* * string s1, s2;};

class kid1 : public base {
public:
* * kid1 () {base ("Child 1", "Table 1");}};

class kid2 : public base {
public:
* * kid2 () {base ("Child 2", "Table 2");}

};

My problem is that each object has all the constants as variables.
Is there any way to improve the efficiency?
Thanks a lot in advance,
Omer.

Before improving efficiency, you should do about the correctness of
your program. Correctness is far important than efficiency. Your code
can't be compiled. You have to use initialization list for
constructing base class subobjects in derived classes objects.

After that, for efficiency improvement, you should think about the
role of base class. I think the base class is redundant.

Regards,
Saeed Amrollahi
Nov 18 '08 #2

P: n/a
On 18 nov, 07:26, Omer <omer...@yahoo.comwrote:
Hi All,
I have two classes that have the same functionality, but with
different constants, e.g. database table name.
I've defined a base class that gets the constants in the constructor,
and maintain the functionality. It works fine, but not very
efficiently.
Here's an example:
class base {
public:
* * base (string &const1, string @const2) {s1 = const1; s2 = const2;}
private:
* * string s1, s2;};

class kid1 : public base {
public:
* * kid1 () {base ("Child 1", "Table 1");}};

class kid2 : public base {
public:
* * kid2 () {base ("Child 2", "Table 2");}

};

My problem is that each object has all the constants as variables.
Is there any way to improve the efficiency?
What exactly do you mean? Those strings belong to the base class part
of derived classes objects. Can you define them to be protected
instead of private? By the way, you should use the initialization list
in the constructor:

class kid2 : public base {
public:
kid2() : base("Child 2", "Table 2") {}
};

In this case it's probably a good idea for the base class constructor
to declare parameters by const-ref. (Again, use the initialization
list.)

class base {
public:
base (string const& const1, string const& const2) : s1(const1), s2
(const2) {}
protected:
string s1, s2;
};

Notice that you don't need the semi-colon (;) at the closing braces of
constructors bodies, function bodies or namespaces.
--
Leandro T. C. Melo
Nov 18 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.