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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:15:19+00:00 2026-05-17T20:15:19+00:00

I have a class, say class AddElement{ int a,b,c; } With methods to set/get

  • 0

I have a class, say

class AddElement{
    int a,b,c;
}

With methods to set/get a,b,c… My question is definitely a logic question – say I implement AddElement as follows:

int Value=1;
Value+=AddElement.get_a()+AddElement.get_b()+AddElement.get_b();

Now imagine I want to do the above except ‘a,b,c’ are now arrays, and instead of ‘adding’ I do scalar addition. At runtime sometimes I need ‘a’ but not ‘b’ or ‘c’, so I could rewrite as:

Value+=AddElement.get_a();

(Of course the += is overloaded to represent a scalar addition… and Value is the same size as a) – Other times I might only need b or c to be added etc…

Is there a way to go about selecting which elements, a,b,c, I want to initialize and later use at runtime? ( i.e. I don’t want to malloc a huge array if I’m not going to use it).

In the end I need a class that has a,b,c and then methods that can operate on any combination of a,b, or c – having the user define what methods they need at runtime (via some kind of flag, or config file).

Currently I’m doing the following:

Value+=AddElement.get_a()*FlagA+AddElement.get_b()*FlagB+AddElement.get_c()*FlagC;

where FlagA=1 if you want to use ‘a’ in the addition or 0 if you don’t want it to be included (The same for FlagB and FlagC). This is costly if the array ‘a’ is very large.

I’m probably just not thinking hard enough, but this problem has been bothering me. If you need me to better define the issue I will try, but I believe this is enough to get my point across.

Edit 2
I also forgot to add that I can’t use any conditionals during the implementation of the addition (this is going to be used in a CUDA kernel and I can’t have any thread diverngance – I was hoping to avoid mentioning CUDA since this is entirely a c++ question)

Edit 3
I believe what I need to do is use virtual functions. I want to call the function in the same manner, except have it execute a case specific function.

Edit 4
I would appreciate if someone took a look at my solution – maybe its too ‘exotic’ and there’s a simpler method to accomplish the same end. Thanks for all the suggestions!

Edit 5
Thanks to another user I looked at the Strategic Design Pattern – and this is exactly the solution I used for this problem. I had never heard of that before and ended up rethinking a problem that has already been done (took a while for someone to mention something about it). So the solution:
Determine Algorithm at Runtime = Strategic Design Pattern.

  • 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-17T20:15:19+00:00Added an answer on May 17, 2026 at 8:15 pm

    You provide your class with a method GetSumOfActiveElements that does just what the name says. You can make this class virtual and create subclasses for each scenario, or have the class manage the memory efficiently in some other way.

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

Sidebar

Related Questions

Let's say I have a class: class Foo { public string Bar { get
Let's say I have one class Foo that has a bunch of logic in
I have a class that contains a dynamically allocated array, say class A {
Say I have class A with class A { final String foo() { //
Say we have: class Base { virtual void f() {g();}; virtual void g(){//Do some
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say you have a class with a Uri property. Is there any way
Let's say I have a class that has a member called data which is
Let's say I have a class in my web app called class Foo. It
Let's say I have a class that implements the IDisposable interface. Something like this:

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.