|
if i have a class of type Student
then which is the right way to write my constructor?
Student(char* Name, char* ID)
OR
Student(char*& Name, char*& ID)
thank you
|
|
|
|
|
I guess the first option is the right one in your particular example.
By passing a char * by reference, you're allowing the function called to modify not only the contents of the pointer, but the pointer itself. For instance:
void foo(char *& p)
{
p="Second";
}
...
char *p="First";
foo(p);
Compare this with
void foo(char *p)
{
p="Second";
}
...
char *p="First";
foo(p);
Hope I made myself clear enough.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
thanks
it might sound stupid to you but like if i read the name and ID in that case i just talked about and let say that i have something like this:
main()
{
char name[32];
char ID[10];
cin>>name>>ID;
Student Bob = Student(name, ID);
}
DO you think that would work?
coz it's giving some sort of error!
C2665 'Student::Student'none of the 2 overloads can convert parameter 1from ype char[32]
|
|
|
|
|
I'm not sure what you are asking but I don't think you want to pass an array as a reference. The reason you are getting the error is the complier thinks try to pass the whole array char[32].
You should pass arrays by address. Otherwise they get copied to the stack when passed and that can eat up a lot of stack space. Also they have to be fixed size so the compiler can allocate space on the stack for them.
I think your real question it the best way to pass information to the Student class. Here are some examples...
class Student
{
public:
Student(char *name, char *ID)
{
strcpy(m_name, name);
strcpy(m_ID, ID);
}
private:
char m_name[32];
char m_ID[10];
}; This is very basic but also very dangerous! Nothing to keep use from writing passed the end of the arrays. This is a little better:
Student(char *name, char *ID)
{
strcnpy(m_name, name, 31);
m_name[31] = '\0';
strncpy(m_ID, ID, 9);
m_ID[9] = '\0';
} This is safe but no way to know if we truncated one of the strings passed in. This will solve that:
class Student
{
public:
Student(LPCTSTR szName, LPCTSTR szID)
{
strName = szName;
m_strID = szID;
}
private:
CString m_strName;
CString m_strID;
}; This is better but there is still a problem. A constructor can not return a value. So no way to pass back a error code if they say, pass in an invalid ID value. So this would be even better:
class Student
{
public:
Student()
{
strName = "";
m_strID = "";
}
BOOL SetInformation(LPCTSTR szName, LPCTSTR szID)
{
strName = szName;
m_strID = szID;
return TRUE;
}
private:
CString m_strName;
CString m_strID;
}; I think you can see that the important thing with arrays is making sure you don't write pass the end of them. Also remember you can't return anything from a constructor. So think twice about what you do with the constructor, like don't allocate memory in it! Note that the last code is not perfect either, if you do something like this:
Student A;
Student B:
.
.
.
B = A; You will have to overload the = operator if you use CString objects. So char arrays may be better but they require a little more care in coding.
Hope this helps.
Jonathan Craig
www.mcw-tech.com
|
|
|
|
|
Hello,
i got a question about the procedure that receives the messages of an application that is confusing me a lot.
Up to now, i have always seen that messages were received in an application via its window, and its windowproc(...). That window proc also receives the input messages... And that's one of the things i find strange, because an application could just use the mouse or the keboard, without having a window.
I admit that inputs have to be asociated with "something" to determine what application has the focus, but an application without window could (for exemple) be activated by a left mouse click on an icon in the tray, and unactivated by a right mouse click anywhere on the desktop...
Well, my question should be something like "can i explicity set a kind of mouseproc(...) ?"
Lets go, another one (; : is it possible to separate the mouse, keyboard and window procs into three separate callbacks, each one receiving the asociated messages ?
Thanks in advance,
Lion.
|
|
|
|
|
First question. Message procs are always associated with a window, so in general you must have a window to be able to process any message. The only exception (I know of) are messages intended to a thread: those are submitted by windows in the context of a thread (i.e. created within that thread) and consumed by the thread itself. These special messages are distinguised by having NULL as their associated HWND parameter. The most prominent example is WM_QUIT (see <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/messques_0tgl.asp">PostQuitMessage</a> .)
Second question. You cannot separate events based on the classification you propose. For one, some messages can be triggered both by keyboard and mouse action, like the clicking on a button.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks Joaquín,
couldn't i achieve that using hooks ?
or hooks are made for other things ?
Cheers,
Lion.
|
|
|
|
|
Oh, didn't think about hooks... Well, maybe hooks can make the difference, you're right. I'm afraid I'm no expert on such things, maybe someone else here can answer you more authoritatively.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
|
...that VC7 doesn't support normal C++ programming. All I hear from people is that they don't want to do managed code or NET so they aren't going to get VC7.
Well, I have been using VC7 for a month now and haven't had to WRITE ONE LINE of managed code or NET code. If you don't want to program for NET, then don't. But that is no reason not get VC7.
There are a few good reasons not to upgrade, but not wanting to have NET forced on you is not one of them.
RANT RANT RANT RANT RANT
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
and better standards support, with more to come, is a good reason for C++ programmers to upgrade.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
Luckily, I have been using STLPort, so the changes to better standard support didn't hurt me. I had my software built using VC7 in 2 hours.
Tim Smith
I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?
|
|
|
|
|
I hear that the debugging support has been improved.
One nice feature is support to debug multiple applications in one instance of the IDE.
Also the way that MFC and ATL have been changed in order to make the CString a little more compatible between the two frameworks.
There are other features that I have heard of, but I have not yet had the chance to use it first hand.
|
|
|
|
|
I have a crap-load of memory leaks (embedding the Python interpreter is a blast...) and I need to set a breakpoint so the code breaks when data at a certain memory location has changed.
It looks like I can do this in the Edit/Breakpoints/Data tab, but it won't let me set this as a breakpoint:
(int) *((int *) 0x12345678)
Any idea why? Any idea how to do this?
J
|
|
|
|
|
Ok, I no longer want to do this. I found a better way.
J
|
|
|
|
|
Hi,
I have a 2 pane splitter wnd (bottom and top) in an SDI app. I wanna make the bottom pane of constant size (let's say: clientrect width x 100, and the top pane: clientrect width x (clientrect Height - 100).
I tried to handle WM_SIZE in bottomview and topview first calculating the required rects and then calling MoveWindow, without any success. (the panes were not repainted, so the underlying desktop was seen in them)
Could you give me please some pointer how to do it properly?
Thanks
R.
|
|
|
|
|
I am doing this in a current project... try this:
you need a member variable of your splitter window, for instance m_MySplitterWnd.
and in your main window add a WM_SIZE handler
void CYourMainWnd::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
if( nType != SIZE_MINIMIZED )
{
int iCyCur=0;
int iCyMin=0;
int iCyNew=0;
CRect ClientRect;
// get size of client window
GetClientRect(ClientRect);
// is the splitter valid?
HWND hWndMySplitter = m_MySplitterWnd.GetSafeHwnd();
if( hWndMySplitter == NULL || !::IsWindow( hWndMySplitter ) )
{
return;
}
// get and set row info for splitter
m_MySplitterWnd.GetRowInfo( 0, iCyCur, iCyMin );
iCyNew = max( iCyMin, (ClientRect.Height() - 100));
m_MySplitterWnd.SetRowInfo( 0, iCyNew, iCyMin );
// force the splitter to recalculate
m_MySplitterWnd.RecalcLayout();
}
}
Hope this works, it's working for me..
Sef Tarbell
"A mind all logic is like a knife all blade, it makes the hand bleed that wields it." --Rabindranath Tagore
|
|
|
|
|
Well I am almost ashamed of asking this...
After striving to understand ATL/WTL/and brand new COM interface from Redmond...
#include <cstdio>
#ifndef __cplusplus
#error Ok, now you are sure that its cpp.... Disable Language extensions ... no
#endif
int f()
{
return 10;
}
int main()
{
int n = f();
char s[n];
}
My Documents\Visual Studio Projects\test\main.cpp(15) : error C2057: expected constant expression
Am I missing something??? Did I not understand a word of the C++ standard? Did I forget to turn on a obscure switch buried somewhere? I know that it is not valid C, but I thought it *was* :? valid C++...
The code below compiles fine on gcc 2.95.3, but I am aware that the GCC compiler has some extensions to everything...
Please clarify,
Regards,
R.
|
|
|
|
|
You're not missing anything. The offending line is simply illegal: in C++ (and in C) arrays must be declared as having constant (i.e. knowable at compile-time) size, except if you allocate them dynamically
char *s=new char[n]; which might not be what you want --it incurs the extra burden of managing dynamic memory. A good combination of these two extremes (contanst-size stack allocation vs. dynamic allocation) is STL's vector , which allows for non constant-size allocation and keeps you away from the error-prone memory management issues.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
PS: The code presumably compiles in gcc as this compiler is supersmart and can deduce that f(n) is a constant value, namely 10. It is a striking fact that it's allowing this, however (and I think it's not standard anyway).
|
|
|
|
|
Joaquin,
Gracias por la respuesta.
Not exactly....
I am aware of the "new" approach
In the char traits template:
/usr/include/g++-3/std/straits.h
static char_type* move (char_type* s1, const char_type* s2, size_t n)
{
char_type a[n];
size_t i;
for (i = 0; i < n; ++i)
assign (a[i], s2[i]);
for (i = 0; i < n; ++i)
assign (s1[i], a[i]);
return s1;
}
Is an example.
According to the array help in VCStudio
decl-specifiers dname [ constant-expressionopt ] ;
So I can understand why the code does not work. However, it is quite shocking that in the STL implementation of the GCC compiler they use this feacure
I do not have the STD here, so I cannot answer ly own question..
R.
|
|
|
|
|
It is in fact quite shocking the piece of code you're showing. To me, this is just illegal. Why don't you go and post this to comp.lang.c++.moderated? There're plenty of C++ gurus out there who know every minute detail about the standard.
There's a copy online of an old C++ standard draft, which I guess serves most of the time as a valid reference (at least for aspects that have remain stable since long). Follow this link for the paragraph defining constant expression. In particular, there's an illuminating sentence that says
"In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used"
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Variable length arrays are a part of C99 standard. You give here a sample code for gcc 3, which support this standard. The C++ prog. language from Microsoft implements a different standard.
|
|
|
|
|
HI
Can any one help how me to create the skin for sound station
thanks and reards
BSR
BSR
|
|
|
|
|
What is sound station ? An example of code for a skinnable app is on CP, just search for mpeg.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
Hi,
My question is about this following code :
char ** ValeurColonne;
ValeurColonne = (char **) malloc(sizeof(char*) * 10);
memset(ValeurColonne,0x00,sizeof(char*) * 10);
for (i=0;i<10;i++)
strcpy(ValeurColonne[i],"Some text");
First question :
How can i use new operator to replace malloc ?
Second question :
This line strcpy(ValeurColonne[i],"Some text"); seem correct but i don't understand why we can write ValeurColonne[i]. I think that ValeurColonne isn't an array.
thanks in advance
|
|
|
|
|