|
We can argue semantics forever but the following remains: which function is called depends on the type the expression is assigned to.
Steve
|
|
|
|
|
Stephen Hewitt wrote: which function is called depends on the type the expression is assigned to.
That is the use of implicit typecast, when implicit typecast the compiler uses the operator of the type, not all the functions which return the type.
And also if a function returns int doesnot restrict to assign to a variable of type long. The function is matched with the prototype and return type is evaluated for the further assignment statement.
implicit typecast (or explicit), takes the corresponding operator and not all the function that returns the corresponding type.
|
|
|
|
|
Well, I agree with Rajkumar R they are, in fact, different methods and not overloads of the same method. However, I have to admit that, as you stated, it is arguable.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
|
Thanks Steve,
Your purpose of the sample shows conversion operator is an exception?
regards,
George
|
|
|
|
|
Yes. You can use this with templates too. From memory MSVC6 chokes on it with templates, but it's legal.
Steve
|
|
|
|
|
Thanks Steve,
Stephen Hewitt wrote: You can use this with templates too
You mean using template parameter (template type parameter) as return type for conversion operator?
regards,
George
|
|
|
|
|
Try this (it crashes on MSVC6, but I believe it should work on more standard compliant compilers):
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Magic
{
template <typename T>
operator T() const
{
return 0.1234012340123401234;
}
};
int main()
{
Magic m;
int i = m;
float f = m;
double d = m;
cout << "i=" << i << ", f=" << f << ", d=" << d << endl;
return 0;
}
Steve
|
|
|
|
|
This is probably a better example:
#include "stdafx.h"
#include <iostream>
#include <typeinfo>
using namespace std;
struct Magic
{
template <typename T>
operator T() const
{
cout << typeid(T).name() << endl;
return T();
}
};
int main()
{
Magic m;
int i = m;
float f = m;
double d = m;
return 0;
}
Make sure RTTI is enabled for this one. Again, it crashes in MSVC6, but I believe it should work with more standard compliant compilers.
Steve
|
|
|
|
|
Thanks Steve,
The output from Visual Studio 2008 is,
int
float
double
i=0, f=0, d=0
I am confused if T is int, what is T()?
regards,
George
|
|
|
|
|
George_George wrote: I am confused if T is int, what is T()?
0. It makes generic programming simpler to allow default constructors on base types.
Steve
|
|
|
|
|
Thanks Steve,
Cool!
regards,
George
|
|
|
|
|
class ABC
{
public:
void MyFunc() { cout<<"hello atl";}
};
void main()
{
ABC *c= NULL;
c->MyFunc();
}
|
|
|
|
|
hello atl.
It won't crash as you expect.
Similar thread crash[^]
modified on Tuesday, March 11, 2008 9:00 AM
|
|
|
|
|
it's not a static function, nor a singleton.
it's just gonna crash because he deferences a NULL pointer...
|
|
|
|
|
Hi toxcct,
please check executing it. Are you getting exception?
And in the link provided in my orginal reply i explained my understanding.
toxcct wrote: it's not a static function, nor a singleton.
AFAIK, most singleton implementation tries to keep the object instance as one for process, but other than that, it is same as normal class instance. It should crash, if its corresponding normal instance crashes.
modified on Tuesday, March 11, 2008 10:06 AM
|
|
|
|
|
You need a compiler, my friend. Otherwise your teorethic point of view will sometimes clash with real world behaviours.
Of course, OP code was more than weird.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
probably something like :
"Segmentation Fault" or any other messages that pops up when you access an unallocated pointer or null pointer.
|
|
|
|
|
"You better pass to managed code."
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Why not try it and find out?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
This is also funny !
class ABC
{
int m_i;
public:
void MyFunc() { _tprintf(_T("ABC %d", &m_i));}
void MyFunc2() { m_i = 3;}
};
int _tmain(int argc, _TCHAR* argv[])
{
ABC* c= NULL;
c->MyFunc(); // OK!
c->MyFunc2(); // ACCESS VIOLATION
return 0;
}
|
|
|
|
|
Really cannot you figure out that? See http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2456326[^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Yes, but both methods use data member ...
|
|
|
|
|
Dieghito wrote: Yes, but both methods use data member ...
No.
MyFunc only takes the address of something which doesn't exist, which is also NULL (I guess, no compiler at home now). You can print any addresses including NULL .
If you change it as _tprintf(_T("ABC %d"), m_i) , it will crash.
Maxwell Chen
|
|
|
|
|
relate
[^]
and
[^]
&m_i = &* (this + offset of m_i) which is (this + offset of m_i) its not dereferencing and your printf format specifier is %d (0(this) + 0(offset of m_i)) is quite enough to work printf.
It is not accessing the value.
|
|
|
|