LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-02-2009, 04:28 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
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?

Thanks.

Last edited by unihiekka; 02-02-2009 at 04:30 AM.
 
Old 02-02-2009, 06:15 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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?
 
Old 02-02-2009, 06:43 AM   #3
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
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.
 
Old 02-02-2009, 02:38 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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;
};
ta0kira
 
Old 02-03-2009, 09:40 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ templated Node class: pointers to different instantated class types jhwilliams Programming 3 08-20-2007 06:20 PM
Derived class inside parent class array, possible? xemous Programming 3 10-17-2006 11:35 AM
In C++, how to write destructor of derived class? ArthurHuang Programming 3 06-04-2006 10:40 PM
C++ - throwing exceptions in derived class constructors? MadCactus Programming 4 08-09-2004 06:29 AM
c++ : regarding (inheritence)base class and derived class edreddy Programming 6 07-31-2002 06:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration