Background
I have a container class which uses vector<std::string> internally. I have provided a method AddChar(std::string) to this wrapper class which does a *push_back()* to the internal vector. In my code, I have to add multiple items to the container some time. For that I have to use
container.AddChar("First");
container.AddChar(...
I have a C++ class that overloads operator[], the array subscript/brackets operator. This is awfully convenient outside of my class, where I can write foo[bar]. However, I can't figure out how to use this notation when I'm implementing methods inside my class.
I know I can write operator[](bar) or this->operator[](bar) but those are f...
Hi,
I would like to write a wrapper class with all operators overloaded such that I can detect when we write/read or modify its contents. For instance:
probe<int> x;
x = 5; // write
if(x) { // read
x += 7; // modify
}
Anyone already did that? If not which operators must I overload to be sure I dont miss anything?
...
Overloading the comparison operator, how to compare if the two variables points to the same object(i.e. not value)
public static bool operator ==(Landscape a, Landscape b)
{
return a.Width == b.Width && a.Height == b.Height;
}
public static bool operator !=(Landscape a, Landscape b)
{
return !(a.Width == b.Width && a.Height == ...
class logger {
....
};
logger& operator<<(logger& log, const std::string& str)
{
cout << "My Log: " << str << endl;
return log;
}
logger log;
log << "Lexicon Starting";
Works fine, but i would like to use a pointer to a class instance instead. i.e.
logger * log = new log();
log << "Lexicon Starting";
Is this possible? If ...
I have next code:
#include <iostream>
#include <algorithm>
#include <map>
#include <iterator>
//namespace std
//{
std::ostream& operator << ( std::ostream& out,
const std::pair< size_t, size_t >& rhs )
{
out << rhs.first << ", " << rhs.second;
return out;
}
//}
int main()
{
std::map < size_t, size_t > some_map;
...
I am trying to overload the global operator new and delete for a performance sensitive application. I have read the concerns described at http://www.informit.com/articles/article.aspx?p=30642&seqNum=3 and the recommendations to use Intel TBB's allocator http://www.intel.com/technology/itj/2007/v11i4/5-foundations/5-memory.htm
Since...
The assignment operator can be declared as
T& operator= (const t&);
in a class, but the arithmetic operators cannot be defined that way. It has to be friend function. I don't understand why? Can you please explain ?
...
Hi everyone
I've got a bit of a problem with a somewhat simple wrapper class I have.
It looks something like this:
public class Wrapper<T>
{
private T _value;
public Wrapper<T>(T value)
{
_value = value;
}
public static implicit operator Wrapper<T>(T value)
{
return new Wrapper<T>(value);
}
public static imp...
Does anyone have a exhaustive list of the names that C#/CLR gives to operators? (Maybe my lack of sleep is kicking in, but I can't seem to find it on Google) E.g. op_Addition, op_Subtraction. Furthermore is there any chance that these would be different in other cultures?
I am trying to create a class that can add/subtract etc. two obje...
Have you ever seen any library/code that overloaded boolean operators, which is said to be evil? and What advantages does it give to the user?
...
Hi, I'm having problems trying to overload the post increment operator in C#. Using integers we get the following results.
int n;
n = 1;
Console.WriteLine(n++); // 1
Console.WriteLine(n); // 2
n = 1;
Console.WriteLine(++n); // 2
Console.WriteLine(n); // 2
But, when I try it using classes, it looks like the objects are exchanged.
cl...
Assignment Operator in C++ can be made virtual. Why is it required? Can we make other operators virtual too?
...
As far as I know there is no way to do this, but I am going to ask just in case someone else knows how to do this. How can I declare a date as a const in Delphi?
The only solution I have found is to use the numeric equivalent, which is kind of a pain to maintain because it is not human readable.
const
Expire : TDateTime = 39895;...
The sentence below is from, The Positive Legacy of C++ and Java by Bruce Eckel, about operator overloading in C++:
C++ has both stack allocation and heap
allocation and you must overload your
operators to handle all situations and
not cause memory leaks. Difficult
indeed.
I do not understand how operator overloading has any...
I'm using a 2D matrix in one of my projects. It's something like it is suggested at C++ FAQ Lite.
The neat thing is that you can use it like this:
int main()
{
Matrix m(10,10);
m(5,8) = 106.15;
std::cout << m(5,8);
...
}
Now, I have a graph composed of vertices and each vertex has a public (just for simplicity of the example)...
note, I am not asking for answers. I simply am curious regarding why things work
I need to implement a priority queue for a printer simulator for a class assignment. After looking at examples on the internet, I noticed that operator< was being overloaded in order to arrange the priority queue correctly.
code in question: java2s prior...
I am trying to create a PredicateBuilder<T> class which wraps an Expression<Func<T, bool>> and provides some methods to easily build up an expression with various And and Or methods. I thought it would be cool if I could use this PredicateBuilder<T> as an Expression<Func<T, bool>> directly, and thought this could be done by having an imp...
Hello,
I am learning C++ and I was wondering if I could gain some insight into the preferred way of creating binary operators that work on instances of two different types. Here is an example that I've made to illustrate my concerns:
class A;
class B;
class A
{
private:
int x;
public:
A(int x);
int getX() con...
Hi,
Im trying to overload the assignment operator and would like to clear a few things up if thats ok.
I have a non member function, bool operator==( const MyClass& obj1, const myClass& obj2 ) defined oustide of my class.
I cant get at any of my private members for obvious reasons.
So what I think I need to do is to overload the assi...