Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6468939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:56:12+00:00 2026-05-25T05:56:12+00:00

I’ve got kind of an issue here with c++ – I’ve got two classes:

  • 0

I’ve got kind of an issue here with c++ – I’ve got two classes:
class vec and class vecD : public vec

class vec overloads pretty much any operator – some of them (as +=) return an object of the class itself for obvious reasons. Now my questions is: is it possible to reuse the overloaded functions of vec for vecD but return a vecD object instead? I mean for sure it’s possible to define all functions virtual and redefine them all over again…but, you know there must be some smoother way?

EDIT: Sorry, sure some code: Is it possible to use inheritance like that? Starting from a templated class? Can I reuse them operators from the parents class?

template<typename TYPE>
class vec{
public:

    TYPE *val;
    int dimension;
public:

    vec();
    ~vec();


    TYPE operator[](int right);
    virtual vec<TYPE>& operator=( TYPE right );
    virtual vec<TYPE>& operator=( vec<TYPE> right );
    virtual vec<TYPE> operator+( TYPE right );
    virtual vec<TYPE> operator-( TYPE right );
    [etc]
};

class vecD : public vec<long>{
public:
    long *X;
    long *Y;
    long *Z;
    long *Xmax;
    long *Ymax;
    long *Zmax;

public:
    vecD();
    ~vecD();

    long getAddress();
    long setAddress( long _X, long _Y, long _Z );


    TYPE operator[](int right);
    virtual vecD<long>& operator=( long right );
    virtual vecD<long>& operator=( vecD<long> right );
    virtual vecD<long>& operator=( vec<long> right );

    virtual vecD<long> operator+=( vecD<long> right );
    [etc]
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:56:13+00:00Added an answer on May 25, 2026 at 5:56 am

    Judging by the comments, this might also interest you:

    //this is used as a common base, so that you don't have to write the gory 
    //details twice.  Only once.
    template<typename TYPE>
    class vec_base{
    protected:
        TYPE *val;
        int dimension;
        vec(); /*protected so only vec can use vec_base, nobody else*/
        ~vec();
        /* I don't see any reason for these to be virtual */
        TYPE operator[](int right);
        vec<TYPE>& operator=( TYPE right );
        vec<TYPE>& operator=( vec<TYPE> right );
        vec<TYPE> operator+( TYPE right );
        vec<TYPE> operator-( TYPE right );
        [etc]
    };
    
    //most types just use this.
    //it's just a simple wrapper around vec_base<TYPE>
    template<typename TYPE>
    class vec : public vec_base<TYPE> {
        //no data
        vec(const vec_base<TYPE>& b) :vec_base<TYPE>(b) {}
        vec(vec_base<TYPE>&& b) :vec_base<TYPE>(b) {}
    public: 
        vec() {}
        vec(const vec& b) :vec_base<TYPE>(b) {}
        vec(vec&& b) :vec_base<TYPE>(b) {}
        ~vec() {}
        vec<TYPE>& operator=(const vec<TYPE>& b) 
        {vec_base<TYPE>::operator=(b); return *this;}
        vec<TYPE>& operator+=(const vec<TYPE>& b) 
        {vec_base<TYPE>::operator+=(b); return *this;}
        vec<TYPE> operator+(const vec<TYPE>& b) 
        {return vec<TYPE>(vec_base<TYPE>::operator+(b));}
        vec<TYPE>& operator-=(const vec<TYPE>& b) 
        {vec_base<TYPE>::operator-=(b); return *this;}
        vec<TYPE> operator-(const vec<TYPE>& b) 
        {return vec<TYPE>(vec_base<TYPE>::operator-(b));}
        /*other overloads that return vec_base<TYPE>*/
        /*all they do is call the base, super easy.*/
    };
    
    //when someone wants <long>, they get this instead
    //which is vec_base, but so much more
    //(not that I know what that is)
    class vec : public vec_base<long>{
    public:
        long *X;
        long *Y;
        long *Z;
        long *Xmax;
        long *Ymax;
        long *Zmax;
    public:
        vecD() : vec_base<long>(), X(nullptr), Y(nullptr), Z(nullptr), /*etc*/ {}
        ~vecD() {delete[] X; delete[] Y; delete[] Z;}
    
        /* vec<long> can have unique functions */
        long getAddress();
        long setAddress( long _X, long _Y, long _Z );
    
        /* some are simple wrappers, some you have to play with X/Y/Z*/
        TYPE operator[](int right)
        {return vec_base<long>::operator[](right);}
        vec<long>& operator=( long right ) 
        {
             vec_base<long>::operator=(right);
             return *this;
        }
        vec<long>& operator=( vec<long> right )
        {
             vec_base<long>::operator=(right);
             X = right.X;
             Y = right.Y;
             Z = right.Z;
             return *this;
        }
        vec<long> operator+=( vecD<long> right );
        [etc]
        /*other overloads all must be overridden*/
        /*if they touch X,Y,Z,etc.*/
    };
    
    int main() {
        vec<int> a; //this is what you already had as vec<TYPE>
        vec<long> b; //this is what you already had as vec<TYPE>
        b.getAddress();  //this is allowed on b, but not a
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
i got an object with contents of html markup in it, for example: string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.