Click here to Skip to main content
15,897,371 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Set the text of spincontrol( Updown control) using sendmessage Pin
Bedke8-Sep-09 0:25
Bedke8-Sep-09 0:25 
QuestionSetWindowPos API doubt Pin
theCPkid7-Sep-09 15:47
theCPkid7-Sep-09 15:47 
AnswerRe: SetWindowPos API doubt Pin
«_Superman_»7-Sep-09 16:01
professional«_Superman_»7-Sep-09 16:01 
QuestionOverloading cast & assignment not enough for ==? Pin
dplong7-Sep-09 11:22
dplong7-Sep-09 11:22 
QuestionRe: Overloading cast & assignment not enough for ==? Pin
CPallini7-Sep-09 11:54
mveCPallini7-Sep-09 11:54 
AnswerRe: Overloading cast & assignment not enough for ==? Pin
dplong7-Sep-09 12:30
dplong7-Sep-09 12:30 
AnswerRe: Overloading cast & assignment not enough for ==? Pin
Stuart Dootson7-Sep-09 14:41
professionalStuart Dootson7-Sep-09 14:41 
QuestionRe: Overloading cast & assignment not enough for ==? [modified] Pin
dplong7-Sep-09 18:39
dplong7-Sep-09 18:39 
Thanks a bunch, Stuart. I realized I had a few more situations to take into consideration, so this is the class now. It works like a charm. Thumbs Up | :thumbsup:
#include <string>
#include <assert.h>

using namespace std;

class Name
{
public:
	Name() { }
	Name(const string & t_) : t(t_) { }
	Name(const char * t_) : t(t_) { }
	Name & operator=(const string & rhs) { t = rhs; return *this; }
	Name & operator=(const char * rhs) { t = rhs; return *this; }
	friend bool operator==(const Name & lhs, const Name & rhs) { return lhs.t == rhs.t; }
	operator const string & () const { return t; }
	operator string & () { return t; }
private:
	string t;
};

int main()
{
	char arr[] = "bananas";
	char *arrP = arr;
	string s = "oranges";
	Name nn1 = s;
	Name n0 = string("apples");
	n0 = s;
	n0 = arr;
	n0 = arrP;
	nn1 = n0;
	Name nn2 = Name(nn1);
	Name n1 = "bananas", n2 = "bananas";
	assert(n1 == n2);
	assert(n1 == string("bananas"));
	assert(n1 == arr);
	assert(n1 == arrP);
	assert(n1 == "bananas");
}

Cry | :(( This solution creates another problem for me, though. I was using the following template so that this behavior could be defined for any class, e.g., int, unsigned, and string:
template <class T, class D> class IdTemplate {
public:
	IdTemplate() { }
	IdTemplate(const T t_) : t(t_) { }
	IdTemplate & operator=(const T & rhs) { t = rhs; return *this; }
	friend bool operator==(const IdTemplate & lhs, const IdTemplate & rhs) { return lhs.t == rhs.t; }
	operator T & () { return t; }
	operator const T & () const { return t; }
private:
	T t;
};

However, the doubling up of the constructor and assignment operator for both string and char * makes this rather difficult because, for example, int doesn't have to consider a "shadow" type like char *. Any ideas on how to generalize the solution so that it may be represented with a template?

modified on Tuesday, September 8, 2009 1:05 AM

QuestionWhy do functions in the C library have short names? Pin
0x3c07-Sep-09 10:58
0x3c07-Sep-09 10:58 
AnswerRe: Why do functions in the C library have short names? Pin
CPallini7-Sep-09 11:13
mveCPallini7-Sep-09 11:13 
AnswerRe: Why do functions in the C library have short names? Pin
dplong7-Sep-09 11:32
dplong7-Sep-09 11:32 
AnswerRe: Why do functions in the C library have short names? Pin
Richard MacCutchan7-Sep-09 11:33
mveRichard MacCutchan7-Sep-09 11:33 
AnswerRe: Why do functions in the C library have short names? Pin
Stuart Dootson7-Sep-09 14:45
professionalStuart Dootson7-Sep-09 14:45 
QuestionID_FILE_NEW causes application crashed [modified] Pin
transoft7-Sep-09 10:58
transoft7-Sep-09 10:58 
AnswerRe: ID_FILE_NEW causes application crashed Pin
Richard MacCutchan7-Sep-09 11:36
mveRichard MacCutchan7-Sep-09 11:36 
GeneralRe: ID_FILE_NEW causes application crashed Pin
transoft7-Sep-09 16:58
transoft7-Sep-09 16:58 
GeneralRe: ID_FILE_NEW causes application crashed Pin
Richard MacCutchan7-Sep-09 23:25
mveRichard MacCutchan7-Sep-09 23:25 
AnswerRe: ID_FILE_NEW causes application crashed Pin
SandipG 7-Sep-09 21:39
SandipG 7-Sep-09 21:39 
GeneralRe: ID_FILE_NEW causes application crashed Pin
transoft8-Sep-09 2:12
transoft8-Sep-09 2:12 
AnswerRe: ID_FILE_NEW causes application crashed Pin
David Crow8-Sep-09 3:31
David Crow8-Sep-09 3:31 
GeneralRe: ID_FILE_NEW causes application crashed Pin
transoft8-Sep-09 3:50
transoft8-Sep-09 3:50 
GeneralRe: ID_FILE_NEW causes application crashed Pin
David Crow8-Sep-09 3:55
David Crow8-Sep-09 3:55 
GeneralRe: ID_FILE_NEW causes application crashed Pin
transoft8-Sep-09 4:24
transoft8-Sep-09 4:24 
GeneralRe: ID_FILE_NEW causes application crashed Pin
David Crow8-Sep-09 4:35
David Crow8-Sep-09 4:35 
GeneralRe: ID_FILE_NEW causes application crashed [modified] Pin
transoft8-Sep-09 4:33
transoft8-Sep-09 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.