|
I'm getting back into programming after a long time off, and may post a few... silly questions. OK, here goes... I'm creating a network simulator, using (among other things), a number of derived classes that share a common base class. Most access to these classes is obtained through the use of a pointer to the base class, accessing the derived class functionality. All well and good. I'm having trouble, when I want to make a copy of one of the derived classes with a pointer to the base class. What I get is a copy of the base class only!
Should I be able to make a copy of a derived class if I'm using a pointer to it's base class? If so, do I need to set up either the base class or the derived class in such a way as to enable this copy function?
Thanks.
|
|
|
|
|
Hmmm
I couldn't reproduce your problem (I am using VC++.NET)
class B
{
public:
B()
{
a=100;
}
int a;
};
class D : public B
{
public:
D()
{
b=200;
}
int b;
};
int _tmain(void)
{
B* b1 = new D();
B* b2 = b1;
D* d = static_cast<D*>(b2);
printf("%d",d->b);
delete b1;
return 0;
}
Regards,
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9
Click here for review[NW]
|
|
|
|
|
|
This is a very common situation when dealing with abstract classes. The usual solution is to define a virtual clone member function in the base class like this:
class Base{
public:
virtual Base * clone()const=0;
...
};
class Derived: public Base
{
public:
virtual Base * clone()const
{
return new Derived(*this);
}
};
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hi, I'm having trouble inserting data into a MySQL table.
char myid[50]="testid";
char myip[50]="testip";
char myname[50]="testname";
char mymessage[50]="testmessage";
mysql_query(&mysql,"INSERT INTO mytable (id, ip, name, message) VALUES('myid','myip','myname','mymessage')")
This should insert the data which my vars contains, but instead it inserts myid myip myname and my mymessage into the table, not the data.
I think my syntax is wrong, since the mysql_query inserts 'myid' and no "testid" as it should..
I need help on this
Oh by the way, I'm not using the MySQL++ wrapper.
Thanks for any help
|
|
|
|
|
Anonymous wrote:
mysql_query(&mysql,"INSERT INTO mytable (id, ip, name, message) VALUES('myid','myip','myname','mymessage')")
That line is all wrong friend.
Use something like this :-
char sqlstr[512];<br />
sprintf(sqlstr,"insert into tablename(id,ip) values ('%s','%s')",myid,myip);
Now do :-
mysql_query(&mysql,sqlstr);
Regards,
Nish
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9
Click here for review[NW]
|
|
|
|
|
Thanks for your help. I never thought of that
|
|
|
|
|
I want to get the IP of the DNS server on a local computer.
is there a general way to do this that will work on all windows flavours?
At First it thought of IPHlpApi.lib, but Ip Helper Library is not supported in win95...
obviously it is possible, because WinIPCFG displays that information...
please help, thanks!
|
|
|
|
|
I think the easiest way is to read it from the registry. But even then you have to read different places on different versions of windows.
- Anders
Money talks, but all mine ever says is "Goodbye!"
|
|
|
|
|
any expert pls kindly help me,
i've created a new control "uiCtrlListCtrl" from the window control "CListCtrl". and i've defined some new message handlers.
such as:
void uiCtrlListCtrl::OnItemchanged(NMHDR* pNMHDR, LRESULT* pResult){
...
CWnd* pwndParent = GetParent();
pwndParent->SendMessage(UI_WM_ITEMCHANGING);
...
}
i have two such new list controls in a dialog(m_lsclList1 and m_lsclList2), how can i tell which control is it from when i received such an UI_WM_ITEMCHANGING message?
my message map is defined as below:
ON_MESSAGE( UI_WM_ITEMCHANGING, OnItemChanging)
the message handling function is defined as:
void uiDlgFrm::OnItemChanging(WPARAM wParam, LPARAM lParam){...}
afraid haven't expressed my problem clear enough: how can i pass something through the message's parameters to know it's from m_lsclList1 or m_lsclList2?
thx a lot!
|
|
|
|
|
In c++,I define a class named Tdate,
I can define object using "Tdate td;","Tdate td1(1);","new Tdate;","new Tdate(1);",
And I know I can't do "Tdate td();" because it looks like function,
But Can I do "new Tdate();"?
And more,in visual basic(sorry I am just for contrast),
"Dim td as new Tdate()" is popularly used,and I did not see "dim td as new Tdate"?
I want to find the answer in some programming books,but they always did not illustrate clearly.
So I want others' help,or could you introduce me some articles or documents that elaborate on object define.
Thanks.
this is my signature for forums quoted from shog*9:
I can't help but feel, somewhere deep within that withered, bitter, scheming person, there is a small child, frightened, looking a way out.
|
|
|
|
|
Tdate td;
and
Tdate td();
are equivilant.
new Tdate td();
and
new Tdate td();
are equivilant.
The compiler is able to recognize they are not function calls by the context they are used in.
In VB, DIM td as new Tdate() creates an (unsized) array of Tdate objects.
Shog9
------
And on the pedestal, these words appear:
"My name is Ozymandias, King of Kings,
Look on my Works, ye Mighty, and despair!"
Nothing beside remains.
|
|
|
|
|
Thanks Shog.
In a book I have just read said "Tdate td()" is not allowed.
Is it right that "Tdate td()" equals "Tdate td",and "new Tdate" equals "new Tdate()"?
In VB,what is the difference of the following three:
Dim td as new Tdate();
Dim td as new Tdate;
Dim td as Tdate;
Thanks.
this is my signature for forums quoted from shog*9:
I can't help but feel, somewhere deep within that withered, bitter, scheming person, there is a small child, frightened, looking a way out.
|
|
|
|
|
zhoujun wrote:
In a book I have just read said "Tdate td()" is not allowed.
As Joaquín also pointed out, that is correct; i was wrong. (i guess i should probably stick to sleeping at 2:00am...)
new Tdate;
is equivilant to
new Tdate();
however.
zhoujun wrote:
In VB,what is the difference of the following three:
Dim td as new Tdate();
Dim td as new Tdate;
Dim td as Tdate;
The first one declares an array, but gives it no initial size (parentheses are used for arrays as well as function calls in VB)
Actually, i'm not possitive that the first line will work; the syntax seems a bit odd, even for VB, as new is used to instantiate objects, but as the array is initially empty, there is nothing to instantiate.
The second one declares and instantiates a Tdate object.
The third one declares, but does not instantiate a Tdate object; it will need to later be instantiated using new or CreateObject() .
Generally in VB6 the use of new in a declaration line is discouraged, as though it's function is to instantiate an object, it does not actually do so until it is first used, at which point it is difficult to gracefully handle failures. In VB.NET, this has been corrected.
(The VB forum would probably be the place to get more confident answers to VB questions...)
Shog9
------
And on the pedestal, these words appear:
"My name is Ozymandias, King of Kings,
Look on my Works, ye Mighty, and despair!"
Nothing beside remains.
|
|
|
|
|
Unfortunately, Tdate td; and Tdate td(); are not equivalent. The latter, as the original poster rightly pointed out, is interpreted by the compiler as the declaration of a function with no arguments returning a Tdate , regardless of the context. It is a quirk of the language we have to live with
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Ooops! Thanks Joaquín; not sure what i was thinking last night...
Shog9
------
And on the pedestal, these words appear:
"My name is Ozymandias, King of Kings,
Look on my Works, ye Mighty, and despair!"
Nothing beside remains.
|
|
|
|
|
Hi,
with regard to UrlEscape function, why do I get "'UrlEscape' : undeclared identifier" when I compile?
I do have the required version of "Shlwapi.dll".
I am running on Win2000.
Kindly Help.
Thanks,
John.
|
|
|
|
|
It's declared in shlwapi.h (5.0 and higher).
She's so dirty, she threw a boomerang and it wouldn't even come back.
|
|
|
|
|
I am programming with MSVC++ 5.0 and 6.0. Interestingly the Shlwapi.dll offers the UrlEscape function, but is not declared in the file Shlwapi.h as posted by Stephane. Perhaps you need the latest SDK.
|
|
|
|
|
Yes, download the Platform SDK[^]. Or buy VC7.
She's so dirty, she threw a boomerang and it wouldn't even come back.
|
|
|
|
|
Now, I've created a control by subclassing it from CStatic.
The thing is :
I wanna place this control on a dialog, and when the user right-click on a certain spot on this control, a pop-up menu should show up.
The problem is : although this subclassed control exposes/supports the WM_RBUTTONDOWN message within it's class, when I wanna use it from the main dialog, ClassWizard shows only the BN_CLICKED message!
Of course, I can add the WM_RBUTTONDOWN message handler to the main dialog class and test if the right mouse button was clicked within this subclassed static control, but the fact is : this message isn't being sent!
I mean, yes. It is sent when I right-mouse click outside the subclassed control, but when I right-mouse clicked inside the control, this WM_RBUTTONDOWN message isn't being sent, so this approach is useless.
I've added the SS_NOTIFY style to this subclassed control, but useless either.
What I wanna do is to "force" this subclassed static control to expose the WM_RBUTTONDOWN message handler, just like the way the BN_CLICKED message handler of this sub-classed static control is exposed. Does anyone have any ideas? Please and thanks a lot!
......
|
|
|
|
|
If nothing else, you can add it to the message map manually using ON_MESSAGE .
Gary R. Wheeler
|
|
|
|
|
Hi.
I am studying more advanced C++ via Scott Meyer's Effective STL. I have not read any of his previous books including the C++ series. He does an exceptional job introducing invaluable advices on the use of STL. I am still reading it.
He makes references to a term that I am not familiar with. He mentions "reference counting" about vector and string containers. What is it?
Thanks,
Kuphryn
|
|
|
|
|
That means that every time you assign or copy a string, the string object does not copy the character data, instead it increments a count that keeps track of how many objects are using its buffer. For example:
string s1 = "foo";
string s2 (s1);
string s3;
s3 = s2; After making those three strings, there is only one copy of "foo" in memory, and all 3 string objects point to it. The string class only copies the character data if you modify a string, such as:
s3 += "bar";
--Mike--
Just released - 1ClickPicGrabber - Grab & organize pictures from your favorite web pages, with 1 click!
My really out-of-date homepage
Sonork-100.19012 Acid_Helm
|
|
|
|
|
I have a quickie question for you:
On modern machines, especially with the memory typically available, how big a difference does it make to have a string class reference counted? Is it truly worth all the effort that goes into making the refcounting work correctly in all situations?
Just curious
¡El diablo está en mis pantalones! ¡Mire, mire!
|
|
|
|
|