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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:11:53+00:00 2026-05-18T05:11:53+00:00

I encounter a problems about override virtual functions, in fact,it is about hessian (a

  • 0

I encounter a problems about override virtual functions, in fact,it is about hessian (a web service protocol).

it has a base class Object, and some derived classes : Long,Int,String,…,all derived classes has a no-virtual function “value”

   class Object  
   {  
     ...    
   };  


   class Long :public Object  
   {  
       ...  
   public:  
       typedef long long  basic_type;  
       basic_type value(){return value_;}  
   private:  
       basic_type value_;  
       ...  
   };  


   class Int :public Object  
   {  
      ...  
   public:  
       typedef int basic_type;  
       basic_type value(){return value_;}  
   private:   
       basic_type value_;  
       ...  
   };  

now I want to add a function ,say, toString ,which can convert Object to a string:

Object *obj  = ...
cout<<obj->toString();

if I can change the value function to virtual ,I only need to write a toString function in Object, else, I need to write a virtual function toString, and to override this functions in all derived classes.

for example

   class Object  
   {  
       virtual Type value(); // It seemed that I can't write a function like this,because the Type is different for different derived classes  


       std::string toString()  
       {  
           some_convert_function(value());  
       }  

   };  

but I can’t write a virtual value function because of return value can’t be override.

is there any good solution for this issue?

Thanks

  • 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-18T05:11:54+00:00Added an answer on May 18, 2026 at 5:11 am

    No, you can’t write toString in Object using a virtual ‘value’ function and override the return type. However you can write a virtual toString and with a template programming trick accomplish almost the same thing.

    class Object
    {
    public:
      virtual std::string toString();
    }
    
    
    template < class ValueType >
    class BasicType : Object
    {
      public:
      typedef ValueType basic_type;
      basic_type value() { return value_; }
    
      std::string toString()
      {
        return some_convert_function( value_ );
      }
    
      private:
      basic_type value_;
    }
    
    typedef BasicType<long long> Long;
    typedef BasicType<int>       Int;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Image resizing web service many web provider have support for php but
I've read quite a bit about initialization of static class variables in c++ and
I'm using phonegap on android. Now I encouter some problems about showing dialog on
While trying to use LINQ to SQL I encountered several problems. I have table
I wrote C++ for 10 years. I encountered memory problems, but they could be
Uhm I'm not sure if anyone has encountered this problem a brief description is
I'm learning C# and I encountered the following problem. I have two classes: base
So, I was sitting in my backyard thinking about Pokemon, as we're all wont
just encountered a problem at dict type subclassing. I did override __iter__ method and
This has been frustrating me for days now. I am trying to use Sharepoint

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.