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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:15:57+00:00 2026-06-01T16:15:57+00:00

Currently building this hierarchy of objects and to be able to make the logging

  • 0

Currently building this hierarchy of objects and to be able to make the logging a bit more clear in the baseclass I decided to have some kind of variable or function that returns the type of the class.

For example:

class fruit {
   string _type;

   fruit() {
       _type = "base"; // or i dont have to set it. however, it wont be inforced
   }

   virtual const char* type() const { return "base"; } // or just = 0 to inforce it

   void function() {
       log(this->type(), " tastes good");
       log(this->_type, "tastes good");
   }
}

class apple : public fruit {

   apple() {
       _type = "apple";
   }

   const char* type() const { return "apple"; }
}

As you can see there is two approaches and me personally think that the return value version is much cleaner since it is clear that it needs to be implemented.

My question however is what approach is the best one? And is it a lot slower to call a function everytime I’m logging something? Just assumed that it would be optimized but I can be wrong.

  • 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-01T16:15:58+00:00Added an answer on June 1, 2026 at 4:15 pm

    First of all, If you need to know the type of class in your program then there is something wrong in your design. Usually, You wouldn’t want to your program to depend on concrete imlpementations but to depend on interfaces.

    Anyhow, the function should be a pure virtual function so that every deriving class must implement it otherwise you would end up getting the wrong types for derived classes if someone doesn’t implement it for their derivying class.

    Assigning a string and returning a string literal are both fine.
    String literals have static storage duration so it will be valid throughout the duration of the program, Also you appropriately have the const charreturn type so that user doesn’t modify the returned literal and end up causing an Undefined Behavior.
    Returning a member is fine as well though it justs add some overhead of an additional member to your class & you are not too worried about this extra member.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently building a project and I would like to make use of some
I'm currently building a query like this: account.sales.method_needing_more_account_info(account) I want to be able determine
I am currently building a site with Expression Engine. I have just put some
With reference to this programming game I am currently building. I have a Class
I have this selectbox on a site Im currently building and what I want
I am currently building in Version 3.5 of the .Net framework and I have
I'm building an hierarchy of objects that wrap primitive types, e.g integers, booleans, floats
I'm currently building an Azure Web Role. I am testing this project against a
I am currently building this Android application, where I will be taking a screenshot
I'm currently building the damage mechanic for my game. Two classes handle this, the

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.