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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:42:03+00:00 2026-06-09T09:42:03+00:00

I have a classA which has a vector< classB* > vObject . class ClassA{

  • 0

I have a classA which has a vector< classB* > vObject.

class ClassA{
public:
     ClassB** operator [] (int index);
private:
     vector<ClassB*> vObject
};

Let’s Assume that vObject is filled with some classB* objects.
What I want to do is to be able to replace classB* elements of vector like that:

classA_obj[3] = classA_obj[5];
classA_obj[1] = classB_obj;

I tried to return a pointer of ClassB Element.
Here is my current operator implementation:

ClassB** ClassA::operator [](int index){
    return &vObject[index]; }

After that i tried the following:

*classA_obj[3] = *classA_obj[5]

The code of doing all the work with just a vector would be:

vector<ClassB*> vObject;
vObject.push_back(new ClassB(ARG1,ARG2));
vObject.push_back(new ClassB(ARG1,ARG2));
vObject[0] = vObject[1];

I’m really confused about this, I thought my code was right but it actually doesn’t work. I would love if someone could tell me what I’m doing wrong.

The above code is just a sample of my actual code.

  • 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-09T09:42:04+00:00Added an answer on June 9, 2026 at 9:42 am

    If you return a reference, you will be able to do the replacement you requested.

    class ClassA{
    public:
         ClassB*& operator [] (int index) { return vObject[index]; }
    private:
         std::vector<ClassB*> vObject
    };
    

    However, the way you have described your usage seems to indicate you can easily change your vector to hold ClassB objects instead of pointers.

    class ClassA{
    public:
         ClassB& operator [] (int index) { return vObject[index]; }
    private:
         std::vector<ClassB> vObject
    };
    

    Then instead of pushing new ClassB into the vector, you just push ClassB:

    vObject.push_back(ClassB(ARG1,ARG2));
    vObject.push_back(ClassB(ARG1,ARG2));
    

    This has the advantage you not needing to explicitly visit your container to delete the pointers. Otherwise, you will need to update ClassA to obey the rule of three.

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

Sidebar

Related Questions

I have a class which has a private attribute vector rectVec; class A {
I have a class that has a vector member variable, which I fill up
I have a class which has a private attribute which is a reference to
I have a class which has two HashSet<String> collections as private members. Other classes
I have created an abstract base class Animal which has public virtual abstract method
I have a class which has a string vector as a variable and a
I have a class A, which has a static vector of objects. The objects
I have a class which has a std::vector of child control pointer. For obvious
In a C++ application, let's say I have a window class, which has several
Suppose I have a class like this: class Foo { std::vector<int> bar; public: std::vector<int>&

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.