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

  • Home
  • SEARCH
  • 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 8147451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:21:23+00:00 2026-06-06T14:21:23+00:00

Consider the following code: struct Foo { Foo operator+(const Foo &rhs) const; // notice

  • 0

Consider the following code:

struct Foo
{
    Foo operator+(const Foo &rhs) const;
    // notice lack of: Foo operator*(const Foo &rhs) const;
};

template <class T>
struct Bar
{
    T x, y;
    T add() const { return x + y; }
    T mul() const { return x * y; }
};

I have two questions:

  1. Can I inherit from Bar<Foo> and override mul() to something meaningful?

  2. Can I inherit from Bar<Foo> without overriding mul() if I never use mul() anywhere?

  • 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-06-06T14:21:25+00:00Added an answer on June 6, 2026 at 2:21 pm
    1. sure
    2. sure

    Templates are really a kind of smart preprocessor, they’re not compiled. If you don’t use something, you can write complete (syntactically correct) rubbish, i.e you may inherit from

    template <class T>
    struct Bar
    {
        T x, y;
        T add() const { return x + y; }
        T mul() const { return x.who cares what-s in here; }
    };
    

    P.S. since your + operator is used in a const function, it should be declared as const too.

    EDIT: OK, not all compilers support this, here’s one that compiles with gcc:

    template <class T>
    struct Bar
    {
        T x, y;
        T add() const { return x + y; }
        T mul() const { T::was_brillig & T::he::slith(y.toves).WTF?!0:-0; }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: struct Foo { }; struct Bar { explicit Bar(const Foo&)
Please consider the following code. struct foo { }; template<typename T> class test {
Consider the following code: #include <iostream> struct foo { // (a): void bar() {
Consider the following code: struct Calc { Calc(const Arg1 & arg1, const Arg2 &
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: struct Foo { mutable int m; template<int Foo::* member> void
please consider the following code: template <typename T> struct foo { template <typename S>
Consider the following code: template<class T, class F> struct X {}; template<class T, class
Consider the following code struct foo { const int txt_len; const int num_len; char
Consider the following code: #include <iostream> template<class T> struct outer { struct inner {};

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.