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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:45:39+00:00 2026-06-14T14:45:39+00:00

My program has a Car and CarManager class that looks similar to the following:

  • 0

My program has a Car and CarManager class that looks similar to the following:

#include <list>

class Car
{
public:
    void Draw() { Draw(m_opacity); }
    void Draw(float opacity)
    {
    }

private:
    float m_opacity;
};

class CarManager
{
public:
    //Draw cars using their m_opacity member
    void DrawCars() 
    { 
        for(auto i = m_cars.begin(); i != m_cars.end(); i++)
            i->Draw();
    }

    //Draw cars using opacity argument
    void DrawCars(float opacity)
    {
        for(auto i = m_cars.begin(); i != m_cars.end(); i++)
            i->Draw(opacity);
    }

private:
    std::list<Car> m_cars;
}

MyApplication::OnRender()
{
    CarManager* pCarManager = GetCarManager();

    //If this condition is met, I want all cars to be drawn with 0.5 opacity.
    if(condition)
        pCarManager->DrawCars(0.5f);

    //Otherwise, draw cars using their m_opacity value.     
    else
        pCarManager->DrawCars();
}

C++ doesn’t allow a non-static member to be used as a default argument, so I have overloaded the Drawing functions. If no argument is supplied, an overloaded version of the function will be called using the class member.

Each car has an m_opacity member which is used for rendering. However, there are certain situations where I would like to specify a value for the opacity that I would like all cars to use. In these cases, I would like m_opacity to be ignored in favor of the value that I provide.

In this example, the rendering code in CarManager::DrawCars() is rather small so repeating the same code with a different call to Car::Draw() isn’t a big deal. But in my actual program, repeating all of the same code is not practical.

This is starting to get messy. Is there a better way to go about this?

  • 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-14T14:45:40+00:00Added an answer on June 14, 2026 at 2:45 pm

    There are several ways to deal with the problem:

    1. As was pointed out in another answer, you could use a special value to indicate that a default should be used. If the data type supports an obvious special value this may be the right solution but it doesn’t work with all types and is actually relatively hard to maintain. I have seen cases in production code where the “impossible” value eventually became possible, resulting in undefined behavior (e.g. yields for bonds were thought to be always positive but it turns out that bonds can have negative yields).
    2. Using an optional<T> which basically bundles a T with an indication on whether the object is actually present deals with one special value. If the choice is, indeed, binary (use the passed opacity or the object’s one), this can work: the default would use an optional<T> indicating that the optional argument is absent and otherwise its value would be use.
    3. A more scalable version is to passing a function which determines the opacity of an object by calling it on a Car object: std::function<double(Car const&)>. The default would be a function which obtains the Car‘s opacity but it could be some other function, including always return a constant.

    Since the third option is a bit more obscure I’ll provide an example below (which assumes that the Car has a member function opacity() returning the Car‘s opacity):

    void Car::Draw(std::function<double(Car const&)> getOpacity
                         = std::mem_fn(&Car::opacity)) {
        opacity = getOpacity(*this);
        // ...
    }
    

    It is now easy to pass in other function objects somehow copying the opacity:

    double ConstantOpacity(Car const&, double value) { return value; }
    double ComputeOpacity(Car const& c, double value) { return (c.opacity() + value) / 2; }
    
    Car* car = ...;
    car->Draw(std::bind(&Car::opacity, _1));        // use the car's opacity
    car->Draw(std::bind(&ConstantOpacity, _1, 0.5); // use opacity 0.5
    car->Draw(std::bind(&ComputeOpacity, _1, 0.5);  // average of the car's opacity and 0.5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program has the following class definition: public sealed class Subscriber { private subscription;
In the following program. public class SerialExample { public static void main(String args[]) throws
My program has a tableView with a list of entries that and you can
My program has a class and that class has an +initialize method. I wonder
My program has to use certain files that reside in another directory. Right now
My program has two threads: Main execution thread that handles user input and queues
For my data structure class, I am trying to write a program that simulates
My program has the following lines, works fine when run from Netbeans, JButton Button_1=new
My program has code that saves attachments, I want these attachments to be transferred
The following program has been running for about ~22 hours on two files (txt,

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.