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

  • Home
  • SEARCH
  • 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 8832997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:40:20+00:00 2026-06-14T08:40:20+00:00

Suppose I have a class as follows: class Solution { public: std::vector<double> x; }

  • 0

Suppose I have a class as follows:

class Solution {    
public:    
    std::vector<double> x;
}

Suppose I have a function as follows:

void function(Solution& sol) {
    // do some calculations on the solution vector
}

Under some circumstances, I will want function to perform calculations directly using the vector x. However, under some circumstances I will want to perform calculations using another vector that is produced by a mapping of the vector x.

Give these possible circumstances, it makes sense to introduce an additional member to the class Solution, but in the first circumstance this additional member will simply refer to x, and in the second circumstance this additional member will itself be another std::vector that is determined by a mapping of some form.

So, ideally I could add a ctor to Solution that creates/defines a member named y either as a std::vector or as merely a reference to x. Then, my function could simply operate directly using y.

How might I do 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-14T08:40:22+00:00Added an answer on June 14, 2026 at 8:40 am

    You can define your function as:

    void function(std::vector<double>& x)
    

    And pass different vectors to it, depending on circumstances

    Edit Using references in ctors

    class Solution 
    {    
        std::vector<double> x;
        std::vector<double>& y; 
    public:    
        Solution(std::vector<double>& _y) : y(_y) { }
        Solution() : y(x) { }
        void function() { /* do work on y*/ }    
    };
    

    This way your function always operates on the same reference, but you can control what data this reference refers to. Note that x and y are now private — this ensures that these members are only used locally via function() method.

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

Sidebar

Related Questions

Suppose I have a diamond inheritance situation as follows: class A{ public: virtual void
Suppose I have class A { public: void print(){cout<<A; }}; class B: public A
Suppose I have the following thread: public class MyThread { public void run() {
Suppose I have a calculator class that implements the Strategy Pattern using std::function objects
Suppose I have a C++ class as follows: class Point { // implementing some
Suppose I have a class class Foo { public: ~Foo() { delete &_bar; }
Suppose I have very basic Task class as follows: public class Task { string
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
Suppose I have a Base class and its derived class Derived as follows: class
Suppose I have: class Foo { ... }; class Bar : public Foo {

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.