Const, copy constructor, collection classes and My Issue !!  
Author Message
UnKnown Nick





PostPosted: Visual C++ Language, Const, copy constructor, collection classes and My Issue !! Top

#include <iostream.h>
#include <vector>

using namespace std;

struct First
{
int data;
First()
{
}
First(First& first)
{
data = first.data;
}
First& operator=(First& first)
{
data = first.data;
return *(this);
}
};


struct Second
{
First first;

Second(){}

Second(Second& second)
{
first = second.first;
}

Second& operator=(Second& second)
{
first = second.first;
return *(this);
}

};
void main()
{
std::vector<Second> vec;
Second sec;
vec.push_back(sec);
}

The problem is solved when const argument is used copy constructor and operator=.

I have two questions now:
1) I dont want to use copy constructor with const argument so can some one suggest any other viable structure from collection classes.
2) Compiler generates a warning when I provide two overload operator= in one class. Why is this so Warning number is C4552.

Thanks


Visual C++12  
 
 
Holger Grund





PostPosted: Visual C++ Language, Const, copy constructor, collection classes and My Issue !! Top

I'm afraid STL containers currently require their element types to be copy-constructible (this requirement will hopefully be somewhat weakened with rvalue references). You may be able to use some kind of indirection that wraps your element type (e.g. a boost::shared_ptr) or use another collection class.

C4552 is "operator has no effect". It's likely a problem in your code and has probably nothing to do with overloading operator=. There's nothing special about it, except the compiler will automatically generate a copy-assignment operator (if possible) even if you provide another overload.

You'll probably want to take a close look at the expression the compiler points you to and verify that it does indeed have an effect.

-hg


 
 
einaros





PostPosted: Visual C++ Language, Const, copy constructor, collection classes and My Issue !! Top

#include <iostream.h>
The problem is solved when const argument is used copy constructor and operator=.

What problem



I have two questions now:
1) I dont want to use copy constructor with const argument so can some one suggest any other viable structure from collection classes.

Why is that


2) Compiler generates a warning when I provide two overload operator= in one class. Why is this so Warning number is C4552.

C4552: "'operator' : operator has no effect; expected operator with side-effect."

If you add some logic to the operator body (like assigning something to the class for which you're overloading the operator), the warning should go away.



 
 
UnKnown Nick





PostPosted: Visual C++ Language, Const, copy constructor, collection classes and My Issue !! Top

Thanks Holger .. I have figured out the issue that I must use copy constructor and operator = with const argument(Second class here). Since the first user-typed class has no copy constructor with const argument(now I am talking about first class), so either I need to use a copy constructor and opeator= with const argument or declare the first class instance in Second class as mutable.

I hope this clears every one !!!

Regarding the warning,I just wanted to know why the warning is generated:
Consider
Temp& operator=(Temp& temp);
Temp& operator=(const Temp& temp);

and wanted to avoid these warning as well. Any help on it

 
 
einaros





PostPosted: Visual C++ Language, Const, copy constructor, collection classes and My Issue !! Top


[...] and wanted to avoid these warning as well. Any help on it

You will avoid those warnings by providing some useful implementation for the operators you overload, such as:

class Foo
{
public:
const Foo& operator()(const Foo& rhs)
{
// x = rhs.x; <- uncomment to remove the warning
return *this;
}

private:
int x;
}

Alternatively, you should also be able to disable the warning by using a pragma:

class Foo
{
public:
// save current warning scheme
#pragma warning(push)
// ignore empty operator warning
#pragma warning(disable: 4552)
const Foo& operator()(const Foo& rhs)
{
return *this;
}
// restore saved warning scheme
#pragma warning(pop)

private:
int x;
}