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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:31:41+00:00 2026-06-13T10:31:41+00:00

Consider the following minimal example. #include <vector> class Data { std::vector<int>& v; public: Data(std::vector<int>&

  • 0

Consider the following minimal example.

#include <vector>

class Data
{
    std::vector<int>& v;
public:
    Data(std::vector<int>& _v) : v(_v) {}
    Data(const std::vector<int>& _v) : v(_v) {} // error!
};

int main()
{
    std::vector<int> v;
    const std::vector<int> c_v;
    Data d(v);
    Data const_d(c_v);

    return 0;
}

This does not compile. The whole output from g++ -Wall below.

const.cpp: In constructor ‘Data::Data(const std::vector<int>&)’:
const.cpp:8:41: error: invalid initialization of reference of type ‘std::vector<int>&’ from expression of type ‘const std::vector<int>’

The reason is clear to me: The const keyword is upset about my cast in line 8. The issue: I really sometimes need the Data class with std::vector, but sometimes with const std::vector. Like having two classes: One for reading into Data, and one for reading from Data. However, I do not like to write two Data classes with almost redundant functions. My questions:

  • Can you give a nice solution to how I can achieve what I try to do in main()? C++11 solutions are pretty welcome, too 🙂
  • Is this the reason for iterator and const_iterator?
  • 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-13T10:31:42+00:00Added an answer on June 13, 2026 at 10:31 am

    const-ness of data members needs to be known at compile time. If you “sometimes” need a reference to a const vector and sometimes non-const, you should create a class hierarchy with the base abstract class containing the common functionality, and inherit it in two Data classes: Data and ConstData. The Data would contain a non-const vector, and ConstData would contain a const vector. This way you would not duplicate any logic, while two separate classes would contain two references of different const-ness.

    Here is an example:

    class AbstractData {
    public:
        // Common functions use vect() and const_vect()
        void common_function1();
        void common_function2();
    protected:
        virtual vector<int>& vect() const = 0;
        virtual const vector<int>& const_vect() const = 0;
    };
    
    class Data : public AbstractData {
        vector<int>& v;
    public:
        Data(vector<int>& _v) : v(_v) {}
    protected:
        vector<int>& vect() const {
            return v;
        }
        const vector<int>& const_vect() const {
            return v;
        }
    };
    
    class ConstData : public AbstractData {
        const vector<int>& v;
        vector<int> temp;
    public:
        ConstData(const vector<int>& _v) : v(_v) {}
    protected:
        vector<int>& vect() const {
            return temp; // You can choose to throw an exception instead
        }
        const vector<int>& const_vect() const {
            return v;
        }
    };
    

    Note that this class hierarchy may need a destructor and various copy constructors, depending on the usage.

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

Sidebar

Related Questions

Consider the following minimal example: #include <iostream> using namespace std; class myostream : public
Consider following example: #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <hiredis/hiredis.h> int main(int argc,
Consider following example: #include <iostream> #include <functional> #include <algorithm> #include <vector> #include <boost/bind.hpp> const
Consider following example. #include <iostream> #include <algorithm> #include <vector> #include <boost/bind.hpp> void func(int e,
Consider following 2 programs giving same error First calss: public class Testing { Testing
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following simple example: Area.hh #pragma once class Area; #include <QScrollArea> class Area :
Consider following program: static void Main (string[] args) { int i; uint ui; i

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.