I am trying to basically do 3 things at once here: overload the assignment operator using a template, restrict the types (using boost::enable_if), and having a specific return type.
Take this as an example:
template <class T>
std::string operator =(T t) { return "some string"; }
Now, according to boost enable_if (sec 3, bullet pt 1)...
I want to overload operator<< for my class. Should I add this overloaded definition to the std namespace? (since the ostream operator<< is part of the std namespace) Or should I just leave it in the global namespace?
In short:
class MyClass {
};
namespace std {
ostream& operator<< ( ostream& Ostr, const MyClass& MyType ) {}
}
O...
I'm trying to implement a structure which is basically a custom made hash table containing either nothing (NULL) or a pointer to a binary search tree object.
Anyway, I'm having trouble figuring out how to do some things, such as setting the hash table, which is an array to NULL, and also memcpy'ing BST objects from one table to another....
I need to read exactly 32 bits from a file. I'm using ifstream in the STL. Can I just directly say:
int32 my_int;
std::ifstream my_stream;
my_stream.open("my_file.txt",std::ifstream::in);
if (my_stream && !my_stream.eof())
my_stream >> my_int;
...or do I need to somehow override the >> operator to work with int32? I don't see t...
Hi. I'm an intermediate C++ user and I encountered the following situation. The class definition shown below compiles fine with a g++ compiler. But I cannot put my finger on what exactly the whole syntax means.
My guess is that the function operator int() returns an int type.
Moreover, I cannot figure out how to use the overloaded op...
Hi i am facing a problem with the code below which is run on VS c++ 2008.How to write the function definition for operator + when you have a statement to be overloaded as follows
class Distance
{
private:
int feet,inches;
};
main......
Distance Obj, Obj1(2, 2);
Obj = 3 + Obj1; // this line here
Obj1+3 is easy but how in this on...
Possible Duplicates:
Solution for overloaded operator constraint in .NET generics
Define a generic that implements the + operator
Hi,
So I have a generic class MyClass<T> and I want to put a constraint that the type T must define an operator, for example public static T operator +(T x,T y).
Is that possible?
If not, I was...
Hi,
I've created a class myString and I'm trying to run the following code:
class myString{
char* str;
int len;
public:
myString(char* str1 = " "){
len = strlen(str1);
str = new char[len+1];
strcpy(str, str1);
};
int getLen() const {
return len;
};
char* getString() const {...
Is this valid?
public struct MyStruct
{
public int Foo { get; set; }
public static bool operator ==(MyStruct a, MyStruct b)
{
return a.Equals(b);
}
public static bool operator !=(MyStruct a, MyStruct b)
{
return !a.Equals(b);
}
}
(I know it's slightly inefficient because Object.Equals uses...
In my code, i have overloaded the new and delete operators to get filename and line number. In my code I am using map and stack. When i do erase a particular value from the map it just call my overloaded delete function but I want only explicit delete statements to be able to access my function, not others. How can I do that?
...
C++ allows overloading operator new - both global and per-class - usual operator new, operator new[] used with new[] statement and placement operator new separately.
The former two of those three are usually overloaded for using customized allocators and adding tracing. But placement operator new seems pretty straightforward - it actual...
Suppose I have a class with overloaded operator new.
class Class {
public:
void* operator new( size_t );
void operator delete( void* );
};
Will objects of that class always be allocated with the overloaded operator new when I use new Class() or is it possible that the default operator new is used when new Class() construct appea...
Hi,
I have a little problem in C++ I don't know how to solve.
The first part of the problem is to access an element in a struct via [], or better, to map [] to a subelement.
My struct looks like this:
struct e {
std::string content;
std::string name;
std::map<std::string, std::vector<e> > elements;
};
If I want to access ...
What is the problem with this code ? this code is giving me lots of syntax errors. Also I would like to know why functors are used in C++.
class f
{
public:
int operator(int a) {return a;}
} obj;
int main()
{
cout << obj(0) << endl;
}
...
I have a class A that I overload its operator=. However it is required that I need to do something like this:
volatile A x;
A y;
x = y;
which raised an error while compiling
error: no operator "=" matches these operands
operand types are: volatile A = A
If I removed volatile, it's compilable. Is there anyway to have this com...
I am working on a small DSL that uses the nomethod fallback for overloading to capture the operators used on the overloaded values. This is similar to the function of the symbolic calculator described in overload's documentation.
This works fine for the standard comparison operators, but consider the following:
my $ret = $overloaded =...
Write an overloaded operator+ function so that two instances of the quadratic class can be added together as in the following code:
quadratic y1 = quadratic (1.0, -5.0, 7.0);
quadratic y2 = quadratic (-3.0, -2.0, 10.0);
quadratic y3;
double result;
y3 = y1 + y2;
result = y1.evaluate (10.0);
cout << result <<endl;
To help you out, her...
So my prof has a sample .h file with the following operators at the end
//ComplexNumber.h
#include <iostream>
using namespace std;
#ifndef MY_COMPLEX_H
#define MY_COMPLEX_H
class complexNumber {
public:
complexNumber();
complexNumber(double a, double b);
void setReal(double a);
void setImaginary(double b);
double ...
I'm implementing a few math classes and for the first time finding problems with Java (cumbersome and duplicated code) that I really can't overcome by cleaver coding. Operator overloading would be REALLY handy, and there are a few other things that Groovy can do a lot better (I may also try Scala at some point).
So I'm still writing th...
hey, i have overridden operator<< and when i'm trying to use it in a print method (const) i'm getting an error :
the overriden operator :
ostream& operator <<(ostream& os, Date& toPrint)
{
return os << toPrint.GetDay() << "/" << toPrint.GetMonth() << "/" << toPrint.GetYear();
}
where i'm trying to use it :
void TreatmentHistory...