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

C / C++ / MFC

 
GeneralRe: Converting DWORD Pin
CPallini17-Mar-10 1:47
mveCPallini17-Mar-10 1:47 
GeneralRe: Converting DWORD Pin
KingsGambit17-Mar-10 1:52
KingsGambit17-Mar-10 1:52 
Questionhow can i get the path of a specific application? Pin
mazizi17-Mar-10 0:00
mazizi17-Mar-10 0:00 
AnswerRe: how can i get the path of a specific application? Pin
Eugen Podsypalnikov17-Mar-10 0:11
Eugen Podsypalnikov17-Mar-10 0:11 
GeneralRe: how can i get the path of a specific application? Pin
mazizi17-Mar-10 0:49
mazizi17-Mar-10 0:49 
GeneralRe: how can i get the path of a specific application? Pin
Eugen Podsypalnikov17-Mar-10 0:58
Eugen Podsypalnikov17-Mar-10 0:58 
QuestionRe: how can i get the path of a specific application? Pin
David Crow17-Mar-10 4:34
David Crow17-Mar-10 4:34 
QuestionUnanticipated behavior from overloaded cast operator... [modified] Pin
Mike the Red16-Mar-10 23:01
Mike the Red16-Mar-10 23:01 
The code:
class testClass {
public:
	testClass() {	m_buff = new BYTE[20]; 
			memcpy(m_buff, "0123456789", 10); 
	};
	~testClass() { delete m_buff; };

	operator LPBYTE () { return m_buff; };

	void show() {
		cout << "m_buff = " << (__int64) (void *) m_buff << "\n\t"; // Show address of m_buff
		for (int i = 0; i < 10; i++) // Show contents of m_buff
			cout << m_buff[i];
		cout << "\n";
	};
private:
	LPBYTE m_buff;
};

int main(int argc, char * argv[])
{
	testClass * foo = new testClass();
	foo->show();
	cout << "foo = " << (__int64) (void *) foo << "\n"; // Show address of foo
	cout << "(LPBYTE) foo = " << (__int64) (void *) ( (LPBYTE) foo ) << "\n\t"; // Show address of cast (LPBYTE) foo
	for (int i = 0; i < 10; i++) // Show contents of cast (LPBYTE) foo
		cout << ((LPBYTE) foo)[i];
	
	cout << "\n";
	delete foo;
	return true;
};


And some output:
m_buff = 9180016
	0123456789
foo = 9179968
(LPBYTE) foo = 9179968
	-garbage-


I was expecting to see:
m_buff = 9180016
	0123456789
foo = 9179968
(LPBYTE) foo = 9180016 // same address as m_buff
	0123456789


What am I missing ?

I know I could accomplish something similar with a member function returning the address of m_buff, but in my ever more ridiculous quest to learn more aspects of C++, I wanted to try this operator overloading bit....

As always, any guidance you can offer is greatly appreciated.

MZR
modified on Wednesday, March 17, 2010 5:11 AMP.S.
My apologies for using __int64... I know it's Microsoft specific, but I use what I've got.


AnswerRe: Unanticipated behavior from overloaded cast operator... Pin
Moak16-Mar-10 23:36
Moak16-Mar-10 23:36 
GeneralThanks, Moak! I didn't understand you at first, but that's exactly the problem. Pin
Mike the Red17-Mar-10 0:24
Mike the Red17-Mar-10 0:24 
JokeRe: Thanks, Moak! I didn't understand you at first, but that's exactly the problem. Pin
Moak17-Mar-10 0:30
Moak17-Mar-10 0:30 
AnswerRe: Unanticipated behavior from overloaded cast operator... Pin
CPallini16-Mar-10 23:45
mveCPallini16-Mar-10 23:45 
GeneralWait.. my overloaded operator isn't being called.... Pin
Mike the Red16-Mar-10 23:59
Mike the Red16-Mar-10 23:59 
GeneralRe: Wait.. my overloaded operator isn't being called.... Pin
CPallini17-Mar-10 0:09
mveCPallini17-Mar-10 0:09 
General-string of expletives removed- I can't believe it was that simple... thanks, Pallini ! -nt- Pin
Mike the Red17-Mar-10 0:16
Mike the Red17-Mar-10 0:16 
GeneralRe: -string of expletives removed- I can't believe it was that simple... thanks, Pallini ! -nt- Pin
CPallini17-Mar-10 0:24
mveCPallini17-Mar-10 0:24 
AnswerRe: Unanticipated behavior from overloaded cast operator... Pin
Cool_Dev16-Mar-10 23:52
Cool_Dev16-Mar-10 23:52 
GeneralRe: Unanticipated behavior from overloaded cast operator... Pin
Mike the Red17-Mar-10 0:13
Mike the Red17-Mar-10 0:13 
AnswerRe: Unanticipated behavior from overloaded cast operator... Pin
Eugen Podsypalnikov16-Mar-10 23:55
Eugen Podsypalnikov16-Mar-10 23:55 
GeneralNo, no garbage there... Pin
Mike the Red17-Mar-10 0:04
Mike the Red17-Mar-10 0:04 
GeneralRe: No, no garbage there... Pin
Eugen Podsypalnikov17-Mar-10 0:06
Eugen Podsypalnikov17-Mar-10 0:06 
Questionchar array prblm Pin
Member 59031016-Mar-10 22:12
Member 59031016-Mar-10 22:12 
AnswerRe: char array prblm Pin
Nuri Ismail16-Mar-10 22:24
Nuri Ismail16-Mar-10 22:24 
AnswerRe: char array prblm Pin
«_Superman_»16-Mar-10 22:25
professional«_Superman_»16-Mar-10 22:25 
AnswerRe: char array prblm Pin
BIJU Manjeri16-Mar-10 22:29
BIJU Manjeri16-Mar-10 22:29 

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.