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 401211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:00:48+00:00 2026-05-12T17:00:48+00:00

How do I get around this? I clearly cannot make the value() method virtual

  • 0

How do I get around this? I clearly cannot make the value() method virtual as I won’t know what type it is beforehand, and may not know this when accessing the method from b:

class Base
{
public:
    Base() { }
    virtual ~Base() { }
private:
    int m_anotherVariable;
};

template <typename T>
class Derived : public Base
{
public:
    Derived(T value) : m_value(value) { }
    ~Derived() { }

    T value() { return m_value; }
    void setValue(T value) { m_value = value; }
private:
    T m_value;
};

int main()
{
    Base* b = new Derived<int>(5);

    int v = b->value();

    return 0;
}

Compilation errors:

error: 'class Base' has no member named 'value'
  • 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-12T17:00:48+00:00Added an answer on May 12, 2026 at 5:00 pm

    This statement:

    int v = b->value();
    

    The variable ‘b’ is being trated like it is an object of Derived<int>.
    So tell the compiler:

    int v = dynamic_cast<Derived<int>*>(b)->value();
    

    Note: If b is not a Derived<int> the result of the cast is NULL.
    So the following would probably be safer:

    Derived<int>*  d = dynamic_cast<Derived<int>*>(b);
    if (d)
    {
        int v = d->value();
    }
    else
    {
        // Error
    }
    

    Alternatively by using references you get a bad_cast exception thrown:

    // Throw bad_cast on failure.
    Derived<int>& d = dynamic_cast<Derived<int>&>(*b);
    int v = d->value();
    

    Or to be nice and obscrure we can do it one line.

    // Assign v or throw bad_cast exception:
    int v = dynamic_cast<Derived<int>&>(*b).value();
    

    But I think you can achieve what you are trying to do with the boost::any

    int main()
    {
        boost::any   b(5);
    
        int    v = boost::any_cast<int>(b);
    
        b        = 5.6; // double
        double d = boost::any_cast<double>(b);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you get around this Ajax cross site scripting problem on FireFox 3?
Has anyone found a way to get around this? Or a better technique to
I have been trying to get around this error for a day now and
Just trying to get my head around Generics by reading this enlightening article by
Why can't you do this and is there are work around? You get this
I'm struggling to get around the 404 errors from asp.net mvc beta when deploying
Is there any way to get around the Oracle 10g limitation of 1000 items
I have a dev, that will get around our code coverage by writing tests
Wanted to get some consensus around a UI feature I'm working on right now.
I was trying to get my head around XAML and thought that I would

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.