Is it legal to do something like this:
?Code:char undo[5]; int main { undo[] = "Undo"; }
Is it legal to do something like this:
?Code:char undo[5]; int main { undo[] = "Undo"; }
No. You can only use initializers when you declare the variable, not later.
(Although, since we're in the C++ forum, you should have a reason for using char[] instead of string.)
That is true, but in this context tabstop meant define, not just declare.Originally Posted by Programmer_P
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
What you can do is one of these . . .
Code:#include <cstring> char undo[5]; int main() { std::strcpy(undo, "Undo"); return 0; }By the way, you could always just try typing something into your favourite editor and seeing if it compiles . . . and if you're at a computer without a compiler (which happens to me sometimes), you no longer have an excuse!Code:const char *undo; int main() { undo = "Undo"; return 0; }
Dinkumware, Ltd. - Compleat Libraries Reference -- the Dinkumware online compiler
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Hey, that's awesome! An online compiler!
And my code actually compiled.
I wrote this:
and then hit Compile, and it compiled successfully!Code:#include <iostream> using namespace std; char undo[5]; //declare char array for the purpose of storing the string "Undo" int main() { char undo[] = "Undo"; //define char array as the string literal "Undo" return 0; }
Here are the results:
So I take it that code is legal after all?Dinkum Exam? page results:
Your code has been compiled with the Microsoft Visual Studio 2005 C++ compiler using
the Dinkum C++ library from the Dinkum Compleat Libraries for VC++ package.
This is the compiler output using the code above in a file named
sourceFile.cpp:
--------------------
sourceFile.cpp
--------------------
size sourceFile.exe :
2048t + 1536 .rdata + 512d = 4096 (1000)
Code compiled successfully!
The executable generated was 5 KB.
Click on the 'Order On-Line' button to buy the library for immediate download.
All rights reserved 2006 by Dinkumware, Ltd.
Copyright (c) 1996-2006 by Dinkumware, Ltd.
Dinkumware, Dinkum, Jcore, and Proofer are registered trademarks of Dinkumware, Ltd.
Now what if I wanted to output the char array string?
Could I do something like this:
Code:#include <iostream> using namespace std; char undo[5]; //declare char array for the purpose of storing the string "Undo" int main() { char undo[] = "Undo"; //define char array as the string literal "Undo" cout<< undo[] <<endl; return 0; }
Last edited by Programmer_P; 06-05-2009 at 10:36 PM.
Ok, so that didn't work, so I changed it to
without the [], and it compiled!Code:#include <iostream> using namespace std; char undo[5]; //declare char array for the purpose of storing the string "Undo" int main() { char undo[] = "Undo"; //define char array as the string literal "Undo" cout<< undo <<endl; return 0; }
Only trouble is I'm not sure what it outputted. Hopefully it was
Undo
Notice that what you compiled and what you originally posted are different. The code that you compiled defines a global array, of 5 chars, named undo, and also defines a local array named undo initialised with "Undo". As such, the local variable hides the global variable of the same name.Originally Posted by Programmer_P
Actually, since the global array is unnecessary, you should just remove it.
You could write:Originally Posted by Programmer_P
Code:cout << undo << endl;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ahh...I was wondering if putting the "char" keyword in int main before the array line made any difference or not. What kind of effect would it have if I had that same array declared in a class instead of as a global array, and then defined it in a function (though not int main)?
For example:
Code:class edit_menu_options_class : public edit_menu_class { public: edit_menu_options_class(); //this will be the constructor for the class ~edit_menu_options_class(); //this will be the destructor for the class protected: char undo[5]; private: void edit_menu_options_mod_function(void); void edit_menu_options_function(); //declare function for manipulating the options in }; //END edit_menu_options_class edit_menu_options_class::edit_menu_options_class(edit_menu_options_mod_function(void)) { //BEGIN brace for edit_menu_options_mod_function undo[] = "Undo"; int main { cout<< undo <<endl; return 0; }
You would get a compile errorOriginally Posted by Programmer_P
The member array would be defined when an object of the class is defined, hence you would not be defining it in the member function, thus that initializer syntax would no longer work.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Damn! That sucks...
I was hoping to avoid having to define all the members of my class in int main...
I'm trying to have every part of my program (or close to it...) in its own file, and call it from inside the int main function in main.cpp. WHY is it not allowed to define a member of a class inside a member function?
In this context, what do you mean by "define"? When you define an object, you define its member variables.Originally Posted by Programmer_P
As in you want to create a new member variable at run time? If you accept that you cannot directly do that due to the rules of C++, then the reason is that the member variables are defined when the object is instantiated. By the time the member function is called, they already exist.Originally Posted by Programmer_P
What I would do is examine if edit_menu_options_class really should have such a member variable. Maybe it should be a local variable. If it should be a member variable, then change it to be a std::string instead, then use assignment in the member function. Or maybe you should be initialising it in a constructor.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Presumably you would want to define each of your objects separately (one object would be "undo", one object might be "Redo", etc.), so your constructor would need to be able to handle different strings.
I think the biggest issues here are your belief that char arrays should be used in C++ in any but the most extreme circumstances and your lack of use of strcpy to assign C-style strings (i.e., char arrays). Why not use a string? Why not use strcpy?
Exactly! And can't you only define (and/or declare) objects inside int main (which would exist in the main source code file)?
The whole point I'm getting at here is I want to define my member variables inside the same file as the class, and so in this particular case, the same file which will store "edit_menu_options_class" which will be separate from main.cpp.
No. As in "I want to define a member of a class inside a member function, and can't!".As in you want to create a new member variable at run time? If you accept that you cannot directly do that due to the rules of C++, then the reason is that the member variables are defined when the object is instantiated. By the time the member function is called, they already exist.
I understand what you're saying and all, I just think it sucks that C++ doesn't allow this...
Ok, so would something like this work?What I would do is examine if edit_menu_options_class really should have such a member variable. Maybe it should be a local variable. If it should be a member variable, then change it to be a std::string instead, then use assignment in the member function. Or maybe you should be initialising it in a constructor.
Nah, there's probably about a billion syntax errors in it...Code:class edit_menu_options_class : public edit_menu_class { public: edit_menu_options_class(); //this will be the constructor ~edit_menu_options_class(); //this will be the destructor protected: std::string undo; private: //add private members here }; //end class edit_menu_options_class::edit_menu_options_class() { undo = "Undo"; } int main { cout<< undo <<; return 0; }
Last edited by Programmer_P; 06-05-2009 at 11:52 PM.