|
It's not working because to the compiler, what you are trying to do is equivalent to this:
class Base
{
protected:
int x;
};
class Derived : public Base
{
public:
void Function()
{
Base b;
b.x = 10;
}
};
Obviously this is incorrect. And, when you tell the compiler that "this" is a "Store" pointer by casting it, you are doing the exact same thing I just showed in this example. Now, if you want to "qualify" the class to which you are referring, to remove an ambiguity, as in the following example, you should use the scope resolution operator.
class Base
{
protected:
int x;
};
class DerivedBase : public Base
{
protected:
double x;
};
class Derived2 : public DerivedBase
{
public:
void Function()
{
Base::x = 10;
DerivedBase::x = 10.0;
}
};
Maybe I'm wrong, but this is how it seems to me...
Chris Richardson
Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
|
|
|
|
|
Thanks for your reply.
I can see your point for needing the class name and the scope resolution operator. It's needed because there are two data members with the SAME name in two different classes, and the only way you can avoid ambiguity in that situation is by specifically qualifying each data member with their respective class name followed by the scope resolution operator.
That is NOT the case in my example. In my sample, only the base class has that particular data member name (there's no ambiguity present). Moreover, the base class is instantiated through the derived class constructor initializer list.
The thing is, I can use just the protected data member name by itself (from a derived class function, even without the base class name and scope resolution operator) and it would be correct!! But if I were to try and access the base class by way of its own 'this' pointer (and qualify it as the base class through casting), suddenly, the compiler starts complaining if I were to try and access a protected member of the base class.
This is where I am. The compiler obviously isn't happy, and it's complaining. I need to know what is there in the rules of C++ that's causing the compiler to report a syntactical error (other than what it's already saying), when another part of the rule is definitely saying differently.
I don't see where I'm violating anything by using the base class 'this' pointer to access one of its protected data member from a derived class.
Maybe I'm suffering from tunnel vision here, but this is the kind of stuff that makes C++ so interesting (or frustrating)!!
William
|
|
|
|
|
The first example illustrates why you can't access the protected base member. The second example was only illustrating the way to avoid ambiguities you had discussed with Alvaro Mendez. Let me see if I can show a better example of why the compiler is complaining.
class Base
{
protected:
int x;
};
class Derived : public Base
{
public:
void Function()
{
Base * pBase = new Base;
pBase->x = 10;
delete pBase;
pBase = (Base*)this;
pBase->x = 10;
}
};
Chris Richardson
Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
|
|
|
|
|
Cool!!! Your explanation makes a lot of sense.
Thanks for your reply!
William
|
|
|
|
|
hello,
I've noticed that when I set a bitmap on a CStatic the previous bitmap don't get destroyed, then I would like to know how to delete the previous image from memory.
Thank you in advance.
NOTE:
Please don't laugh too much while answering...
|
|
|
|
|
::DeleteObject will delete any GDI object such as a HPen or HBitmap. If you're using MFC and the bitmap is in a CBitmap, then it's going to be deleted for you.
Joan Murt wrote:
Please don't laugh too much while answering...
I'm not laughing - I might laugh if you ask this again and again over the next few months, as that would indicate an inability to learn. But every single API, every single keyword, every single bit of knowledge we have, someone had to show us, either indirectly through a web site or book, or directly as I am now. There is no shame in wanting to learn, there is shame in not being willing to be told.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not
as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002
|
|
|
|
|
|
You should only delete a bitmap if you created it. When you create a memory DC, it automatically has a default bitmap in it that windows will manage for you, I believe the same is true for the CStatic that you are talking about.
In order to delete a HBITMAP use ::DeleteObject(HBITMAP). There is one catch though, the bitmap cannot be selected into a memory DC otherwise the function will fail. So that is why you need to select the old bitmap into the memory DC before you call DeleteObject on the bitmap. Or you can delete the memory DC before you delete the bitmap, that will effectively unselect the bitmap from the memory DC as well.
Good Luck
Build a man a fire, and he will be warm for a day Light a man on fire, and he will be warm for the rest of his life!
|
|
|
|
|
|
Hi everyone,
i have an existing code that i need to revise, in this code i used gotoxy() function to set cursor locations in my console. When i try to compile this code in Visual C++ 6.0 (console application) i get an error "error C2065: 'gotoxy' : undeclared identifier" why am i getting this? i'm sure that the conio.h is in my system and that it is included in my source code. any idea why i get this error? and what will be the solution for this?
Thanks/Regards
Sidney
Thanks/Regards
Sidney
|
|
|
|
|
Moving the code over from Borland? It's been a really really long time since I had to manipulate the cursor position of the console, so I can't remember if there's any C++ standard stuff to do it, but in Win32, you can use SetConsoleCursorPosition, which the C++ routine would probably call anyways.
Chris Richardson
Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
|
|
|
|
|
Not actually moving, i just dont have a Borlang compiler right now and i was wondering if i can compile my code using Microsoft Visual C++ 6.0. Well ok let me try replacing the gotoxy with the SetConsoleCursorPosition, Thanks Chris! I really appreciate your help
Thanks/Regards
Sidney
|
|
|
|
|
No problem , I hope it works for ya!
Chris Richardson
Programmers find all sorts of ingenious ways to screw ourselves over. - Tim Smith
|
|
|
|
|
Hi,
Quick question that is probably easy to implement.
I have an MFC Grid control as part of a dialog application. Problem is once it has focus, the user cannot tab-key out of it.
What needs to be done to enable this?
Some example code would be very much appreciated.
Thanks,
-Chris
|
|
|
|
|
Here's what I came up with for the above problem. Is there a more efficient or nicer way to solve this problem?
typedef struct _List
{
int data;
_List* pNext;
_List(int d) : data(d), pNext(0) {}
} List;
int main(int argc, char* argv[])
{
List* pList = new List(0);
List* pTemp = pList;
for (int i = 1; i < 10; i++)
{
pTemp->pNext = new List(i);
pTemp = pTemp->pNext;
}
pTemp = pList;
while (pTemp)
{
DEBUGF("%i", pTemp->data);
pTemp = pTemp->pNext;
}
DEBUGF("reversing...");
List* p1 = pList;
List* p2 = pList->pNext;
p1->pNext = 0;
while (p2)
{
List* p3 = p2->pNext;
p2->pNext = p1;
p1 = p2;
p2 = p3;
}
pList = p1;
pTemp = pList;
while (pTemp)
{
DEBUGF("%i", pTemp->data);
pTemp = pTemp->pNext;
}
return 0;
}
Todd Smith
|
|
|
|
|
Looks good to me. It executes in O(n) so it will be hard to beat.
Then again, I could be wrong.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Hy everybody!
how are you?
I have a litle doubt...
Does have any way to use a Dialog (or something like this) from a DLL into an FormView (inside a SplitterWnd)?
thanks all,
c'ya!
-=Ricky=-
|
|
|
|
|
How could I manage a app according with the user profile logged in a Server Operational System ??
I need functions to get the current profile name and get the directories of all user profiles.
Thanks,
Cris.
|
|
|
|
|
Hi!
I have derived my own class from CAsyncSocket and defined OnReceive/OnSend/On...() -Handlers for any possible events, e.g. like that:
void CUDPSocket::OnReceive(int nErrorCode)
{
if (nErrorCode == 0)
((CServerQueryDlg*)m_pWnd)->OnReceiveUDP();
}
Now I want to put this derived class into a DLL to use it easily in any other application. But I don't know how to make it flexible so that any CWnd -derived class will be accepted in the DLL and not only CServerQueryDlg . Since when I don't cast the call to OnReceiveUDP the compiler can't find the function in my CWnd -based app.
Any suggestions how to make this code flexible?
regards
Greg
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hello,
When I build my MFC project at warning level 4 using VS6sp5, the compiler generates a ton of warning messages from the locale files xlocale and xlocnum. Is this a normal occurance ? I don't want to see these warnings every time I do a rebuild, so how can build my own project files at warning level 4 but outside includes at level 3 ? Or is there a better solution ?
An example of the compiler messages I receive is shown below.
Thank You
srs
c:\program files\microsoft visual studio\vc98\include\xlocnum(599) : warning C4018: '<' : signed/unsigned mismatch
c:\program files\microsoft visual studio\vc98\include\xlocnum(588) : while compiling class-template member function 'class std::ostreambuf_iterator<unsigned short,struct std::char_traits<unsigned short> > __cdecl std::num_put<unsigned short,
class std::ostreambuf_iterator<unsigned short,struct std::char_traits<unsigned short> > >::_Iput(class std::ostreambuf_iterator<unsigned short,struct std::char_traits<unsigned short> >,class std::ios_base &,unsigned short,char *,unsigned int)'
c:\program files\microsoft visual studio\vc98\include\xlocnum(606) : warning C4018: '<=' : signed/unsigned mismatch
c:\program files\microsoft visual studio\vc98\include\xlocnum(588) : while compiling class-template member function 'class std::ostreambuf_iterator<unsigned short,struct std::char_traits<unsigned short> > __cdecl std::num_put<unsigned short,
class std::ostreambuf_iterator<unsigned short,struct std::char_traits<unsigned short> > >::_Iput(class std::ostreambuf_iterator<unsigned short,struct std::char_traits<unsigned short> >,class std::ios_base &,unsigned short,char *,unsigned int)'
c:\program files\microsoft visual studio\vc98\include\xlocale(204) : warning C4244: '=' : conversion from 'unsigned int' to 'char', possible loss of data
c:\program files\microsoft visual studio\vc98\include\xlocnum(151) : see reference to function template instantiation 'int __cdecl std::_Getloctxt(class std::istreambuf_iterator<char,struct std::char_traits<char> > &,class std::istreambuf_it
erator<char,struct std::char_traits<char> > &,unsigned int,const char *)' being compiled
c:\program files\microsoft visual studio\vc98\include\xlocale(207) : warning C4244: '=' : conversion from 'unsigned int' to 'char', possible loss of data
c:\program files\microsoft visual studio\vc98\include\xlocnum(151) : see reference to function template instantiation 'int __cdecl std::_Getloctxt(class std::istreambuf_iterator<char,struct std::char_traits<char> > &,class std::istreambuf_it
erator<char,struct std::char_traits<char> > &,unsigned int,const char *)' being compiled
|
|
|
|
|
Fixed.
My solution to this problem was to change the global warning level before including the headers that used the locale files, like so :
#pragma warning(push, 3)
#if !defined(_BITSET_)
# include <bitset>
#endif // !defined(_BITSET_)
#pragma warning(pop)
|
|
|
|
|
I need a xml filter for Indexing services. There are any chances to find one that is working ?
Do you have experience (positive / or ..) using this filter ?
Thanks a loot .
|
|
|
|
|
Is there a way to automatically zoom the CDC of a window?
ScaleViewportExt sounded like that, but didn't seem to do anything at all :/
|
|
|
|
|
here's a quick and dirty way to do it..
put this at the top of your OnDraw/OnPaint
int lpx = pDC->GetDeviceCaps(LOGPIXELSX);
int lpy = pDC->GetDeviceCaps(LOGPIXELSY);
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetWindowExt(CSize(lpx * (100.0 / (double)m_fZoomPct) , lpy * (100.0 / (double)m_fZoomPct)));
pDC->SetViewportExt(lpx, lpy);
-c
Please stand by
|
|
|
|
|
how i can make dll send message to application
salah
gis member
|
|
|
|
|