ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Rep:
C++: derived class?
Hi guys!
I have written my own Vector class template on the same lines as valarray, but it supports arbitrary precision and has some additional class functions that I need. Vectors are now declared as
Code:
Vector <long double,10> vec;
for example, where vec has 10 elements of type long double.
However, I am now working on a new class template that should use these "Vectors". The new class template should do some operations with these vectors. Should this new class be a derived class then? I am slightly confused about when to make something a derived class. If not, how should I parse these vectors to the new class?
What kinds of things are you going to use this new class for? Can you give examples?
Derived classes are specialisations of the base classes they're derived from and usually have more specific features. For example, both cats and dogs are types of animals, but they're (very) different. Do your classes fit this model at all?
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273
Original Poster
Rep:
No, not really, but never mind. I have solved the problem. I was having some issues with typedefs, which made me think that I might need inheritance, which I did not.
You should generally have a base class for Vector that doesn't have the numerical template argument, then fill it with pure-virtual functions for everything that isn't restricted by the numerical specification. You can then define less-specific operations in terms of this base class.
Code:
template <class Element>
struct VectorBase
{
//general function for all length specializations
virtual unsigned int vectorSize() const = 0;
};
template <class Element, unsigned int Length>
struct Vector : public VectorBase <Element>
{
unsigned int vectorSize() const { return Length; }
//cannot use with different sizes
Vector operator + (const Vector&) const;
};
Speaking in broad-brush terms, you'll find that there are two general ways that you can "combine" functionality in an object-oriented system:
(1) Inheritance and subclassing work straight down the family tree. You get all of what your gene-pool gives you, plus what you change. But you must be "part of the family."
(2) Mix-ins let you deviate from that straight-inheritance approach. You can take any set of existing functionality and "mix in" other functionality ... the idea being that those "mixins" can be "mixed in" to almost anything, "regardless of who its daddy is."
Ultimately, at runtime, it comes down to the same basic thing: every object is "a chunk of data, with a method-pointer table somehow associated with it." What's in that method-table determines exactly what will happen when you ask a particular object to do a particular thing. Inheritance derives the method-table strictly from the parent class; mix-ins inject more specific changes into it.
"There are other ways, obviously." I intended to simplify.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.