returning reference to a vector from a method and using its public members

I have a vector t_vec that stores references to instances of class Too. The code is shown below. In the main , I have a vector t_vec_2 which has the same memory address as B::t_vec.

But when I try to access t_vec_2[0].val1 it gives error val1 not declared.

Could you please point out what is wrong? Also, if you know of a better way to return a vector from a method, please let me know! Thanks in advance.

class Too { public: Too(); ~Too(){}; int val1; }; Too::Too(){ val1 = 10; }; class B { public: vector<Too*> t_vec; Too* t1; vector<Too*>& get_tvec(); B(){t1 = new Too();}; ~B(){delete t1;}; }; vector<Too*>& B::get_tvec(){ t_vec.push_back(t1); return t_vec; } int main(){ B b; b = B(); vector<Too*>& t_vec_2 = b.get_tvec(); // Getting error std::cout << "\n val1 = " << t_vec_2[0].val1; return 0; }

-------------Problems Reply------------

You have 2 errors: The first was already said, you should write


instead of


The second is the strange line b = B();

I think you should delete it. The error occurs because right part B() is gonna be delete just after it is created. So you don't get in the object b the 10 value as you want. Just delete this line and it'll be ok!

But when I try to access t_vec_2[0].val1 it gives error val1 not declared.

That's because your vector is holding pointers, not Too objects. Replace the dot (.) operator with the arrow (->) operator, like this:

std::cout << "\n val1 = " << t_vec_2[0]->val1;

As a general rule though, you should probably avoid having public members at all.

Category:c# Views:0 Time:2010-04-27
Tags: c# stdvector

Related post

  • Return reference to a vector member variable 2011-12-05

    I have a vector as member in a class and I want to return a reference to it through a getVector() function, so as to be able to modify it later. Isn’t it better practice the function getVector() to be const? However I got an error “qualifiers dropped

  • How to return a std::vector from a method and not have it be a copy every time? 2011-12-07

    I'm pretty sure this is some pointer business, but I've been out of C++ for a while. Basically, I have a class with a method getVector() that returns a std::vector it has stored as a class variable. I want to access these elements, but not copy the w

  • returning reference from method 2012-03-13

    class A : boost::noncopyable{ }; class B{ A & conn() const; }; How would i declare and implement conn() given that: conn should create and return a reference to an object of type A. i can't break B's interface in client code using B. i want to pr

  • Returning references from a C++ methods 2010-07-06

    Dear friends, i'm concerned if i'm making a bad use of references in C++ In the following method GCC complains warning "reference to local variable ‘me’ returned" MatrizEsparsa& MatrizEsparsa::operator+(MatrizEsparsa& outra){ MatrizEsparsa me

  • Do methods which return Reference Types return references or cloned copy? 2010-10-16

    I've been learning Java these days and what I've read just is "Be careful not to write accessor methods that return references to mutable objects" which is really interesting. And now I am wondering whether it is same for Properties and Accessor meth

  • How can return reference to Dictionary from a method as readonly? 2009-03-05

    I want to let users iterate through my dictionary without modifying it. I considertwo solutions: 1.ReadOnlyDictionary -- is there an implementation of that available? 2.Copy the entire dictionary -- what is the most efficient way to copy it ? Thanks

  • return underlying array from vector 2009-03-03

    Will the array be deallocated and if so, what is a workaround? double * GetArrayFromVector( std::map<std::string, double> m, char ** names, int count ) { if(!names) return 0; std::vector<double> vec(m.size()); for (int i=0; i<count; ++

  • Is it a good idea to always return references for member variable getters? 2010-09-19

    If I have a class that has many int, float, and enum member variables, is it considered efficient and/or good practice to return them as references rather than copies, and return constant references where no changes should be made? Or is there a reas

  • Learning C++: returning references AND getting around slicing 2010-12-10

    I'm having a devil of a time understanding references. Consider the following code: class Animal { public: virtual void makeSound() {cout << "rawr" << endl;} }; class Dog : public Animal { public: virtual void makeSound() {cout << "

  • function returning an iterator of vector 2009-04-08

    I have this new class Seq that inherits vector and has some additional features. I can use all methods of vector with Seq. Having this data structure: Seq< vector<int> > sweepEvents; I want to have a function that takes an element vector

  • warning: returning reference to temporary 2009-08-27

    I have a function like this const string &SomeClass::Foo(int Value) { if (Value < 0 or Value > 10) return ""; else return SomeClass::StaticMember[i]; } I get warning: returning reference to temporary. Why is that? I thought the both values

  • PHP Returning References 2009-10-05

    Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return refere

  • How to Increment a class Integer references value in java from another method 2010-02-05

    package myintergertest; /** * * @author Engineering */ public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { //this one does not increment Integer n = new Integer(0); System.out.println("n=" + n)

  • C++ best practice: Returning reference vs. object 2010-03-23

    I'm trying to learn C++, and trying to understand returning objects. I seem to see 2 ways of doing this, and need to understand what is the best practice. Option 1: QList<Weight *> ret; Weight *weight = new Weight(cname, "Weight"); ret.append(w

  • Would the instance reference parameters passed into the static methods get garbage collected? 2010-04-21

    I know that the static objects in .Net managed world are loaded in Loader Heap which is never going to be garbage collected. What happens to the instance reference parameters passed to a static methods. Are they get garbage collected once the static

  • c++ return reference / stack memory 2010-07-15

    This question already has an answer here: Can a local variable's memory be accessed outside its scope? 16 answers A basic question that I'm not sure of the answer. Is the follow function valid? std::vector<int> & test_function() { std::vect

  • How to return a 2 dimensional vector? 2010-08-25

    I have a function that creates a 2D vector void generate(int n) { vector< vector<int> > V (n, vector<int>(1 << n , 0 )); ....... }//n is used to determine the size of vector Now, I need to return the created vector to use it i

  • How-to return a const std::vector? 2010-12-15

    I have a class with a container (containing pointer) as a member: MyClass{ private: std::vector<MyObject*> _VecMyObjs; public: const std::vector<MyObject* const> GetVecMyObj(); } Now I try to implement GetVecMyObj(). Here is what I came u

  • C++ Returning reference to local variable 2011-01-10

    Is the following code (func1()) correct if it has to return i? I remember reading somewhere that there is a problem when returning reference to a local variable. How is it different from func2()? int& func1() { int i; i = 1; return i; } int* func

Copyright (C), All Rights Reserved.

processed in 0.167 (s). 11 q(s)