|
This is the beauty of references!
It refers to an instance of an object at a certain address, it's not an actual instance of an object. In this case the address is NULL, which is perfectly legitimate.
However, if you try to use any of its members it would of course crash since the 'this' pointer is NULL.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Deferencing is accesing value, indirectly through pointer.
In your example I say it is not deferencing, its referencing.
int val = *ptr; // accesses value;
int &ref = *ptr; // don't accesses value;
int *ptr2 = &*ptr1; // here Value of (*ptr1) is not accessed, as ptr2 is not going to store the address the temporary value (*ptr1), instead stores address itself (ptr1).
similarly
int &ref = *ptr; is not dereferencing the value but referencing the memory location;
"A reference holds the address of an object, but behaves syntactically like an object." from msdn;
int *ptr2 = &*ptr1; is same as int &ref = *ptr; check the code generated for the two expression.
and "int *ptr1 = NULL; int *ptr2 = &*ptr1" also won't crash.
|
|
|
|
|
Not sure if this is applicable here. If you change the original class as
class SingleTon
{
public:
static SingleTon GetInstance()
{
return *ptr;
}
private:
static SingleTon* ptr;
};
SingleTon* SingleTon::ptr = NULL;
int _tmain()
{
SingleTon ref = SingleTon::GetInstance();
if(&ref == NULL)
{
cout << "The reference is NULL";
}
return 0;
}
The program still doesn't crash, but it doesn't prints anything since in this case
&ref != NULL
And even if I added another member function and accessed it through the "ref" variable, the program still didn't crashed in the release mode and in fact it gave me the correct values
Thanks
|
|
|
|
|
ahh, that is empty class, put data member. when you dereferencing no data is accessed in empty class.
try this,
class SingleTon
{
char m_c;
public:
static SingleTon GetInstance()
{
return *ptr;
}
private:
static SingleTon* ptr;
}; it will crash
and
class SingleTon
{
char m_c;
public:
static SingleTon& GetInstance()
{
return *ptr;
}
private:
static SingleTon* ptr;
}; it won't crash even with data member, my reply is applicable here also.
|
|
|
|
|
Thanks Rajkumar for the explaintion
But I do have more queries. If I add a member method like this
class SingleTon
{
char m_c;
public:
static SingleTon &GetInstance()
{
return *ptr;
}
std::string GetClassName()
{
return std::string("SingleTon Class");
}
private:
static SingleTon* ptr;
};
and access it like this
SingleTon& ref = SingleTon::GetInstance();
std::string className = ref.GetClassName();
cout << className.c_str();
The program doesn't crash and it prints the correct value. Can you please explain why it should print the correct value? Now we have a member variable also. Or is it the case that the since the method is not accessing the data member variable, it is not crashing and it is treated similar to empty class scenario?
Thanks
|
|
|
|
|
psychedelic_fur wrote: Or is it the case that the since the method is not accessing the data member variable
yes,
you can also do the similar code.
(*(SingleTon *)0).functionAccessNoDataMember();
(*(SingleTon *)0).functionAccessDataMember(); // crashes
calling a member function is not dereferencing, member function is called as if a normal function with storing the object address to register which is called this pointer, whenever a data member is accessed the this pointer is dereferenced like
*(this + data member offset) // causes access violation if this pointer is not valid address.
calling member function is not crashing because code is same for all instance of object and function pointer is same no need to call like (this + memberfunction)();
|
|
|
|
|
i need to get the time zone string currently set by the user
e.g. (GMT+5:30)..........
i dont want to do any calculations just need the string
Thanks in advance
|
|
|
|
|
You can use GetTimeZoneInformation().
|
|
|
|
|
hi,
I have a namespace extension under common file dialog on windows xp.
The 'Create New Folder' icon on the dialog's toolbar becomes disabled, when I choose the namespace icon in the upper combobox (Drop-down combo box that displays the current drive or folder, cmb2).
My namespace extension has a custom view, maybe this causes the problem?
Thanks,
zuma
|
|
|
|
|
Hi again,
I'm using UNICODE char set and have this error of converting
char[260] to LPWSTR and...
CString to LPCSTR
I'm not familiar with UNICODE but i have to use it.
Hope you can help me.
Thanks.
|
|
|
|
|
why don't you just always use CString with LPCTSTR ???
when you handle literals, just round them with the _T() macro...
that way, you don't even have to think to convert from ascii to unicode and vice versa...
for example:
CString str = _T("Hello World !");
|
|
|
|
|
Change into property settings ...use
project->configuration property->general
see, character set change into multibyte characterset..
then your code is running proper..
*****THANKS N ADVANCE****
Mathen.K
(I WILL TRY MY LEVEL BEST )
|
|
|
|
|
TooShy2Talk wrote: I'm not familiar with UNICODE but i have to use it.
rowdy_vc++ wrote: see, character set change into multibyte characterset..
Are you answering queries without even understanding them?
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
go round a problem won't fix it.
the OP has a problem of design in his code, and he'd better fix that first...
|
|
|
|
|
We have seen many posts of questions by you. We know your level.
Maxwell Chen
|
|
|
|
|
But if i have to change it to MultiByte char other codes have to be converted to MultiByte.
Changing those codes are more work than fixing this conversion.
I just download sample source code and combine them altogether but problem such this arises due to this.
Hope you can help me.
Thanks.
|
|
|
|
|
read the other replies.
this guy answered you, but he doesn't even know what he talks about.
so don't take his post in consideration; it's useless.
|
|
|
|
|
Since you have to adapt different pieces of code, IMHO the best approach is to uniform the whole project to generic text mappings [^], as already suggested by toxcct. It should be not such a daunting task (hopefully).
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
|
|
|
|
|
When I run the program the output is:
Foo of derived 2
Foo of derived 2
Foo of derived 2
Why is it so? Shouldnt the output be:
Foo of derived 2
Foo of derived
Foo of base
#include<iostream.h>
class CBaseX
{
public:
virtual void foo()
{
cout<<" Foo of base";
}
};
class CBaseY:public CBaseX
{
public:
void foo()
{
cout<<" Foo of derived";
}
};
class CBaseZ:public CBaseY
{
public:
void foo()
{
cout<<" Foo of derived 2";
}
};
void main()
{
CBaseX* bx = new CBaseZ();
bx->foo();
CBaseY* by=dynamic_cast<CBaseY*>(bx);
by->foo();
CBaseX* bx1=dynamic_cast<CBaseX*>(bx);
bx1->foo();
}
|
|
|
|
|
because
Member 4090406 wrote: virtual void CBaseX::foo()
foo is virtual function
|
|
|
|
|
Nope. The output is correct. That's all the magic of polymorphism.
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
|
|
|
|
|
Hi all,
I need to use SQL server via the internet in C++ app, 2 questions:
1) what info do I need? such as domain name, ip address, database name etc.
2) actural C++ code or C++ samples
thx
|
|
|
|
|
hi everyone,
i am savitri here.i am having one more problem that is how to set the cursor to the first block or first text bor in the form. like in VB we use set focus and get focus but how to do in VC++. please help me out.
thanku in advanced,
savitri
|
|
|
|
|
|
SetFocus function [^] if you're using Win32 API or SetFocus method [^] if you're using MCF .
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
|
|
|
|