Thread: Defining char arrays as string literals

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    And can't you only define (and/or declare) objects inside int main (which would exist in the main source code file)?
    No, you can define them in other functions as well (including member functions), or if they are to have static storage duration, outside of functions too.

    Quote Originally Posted by Programmer_P
    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.
    I think that instead of "define" you want to "initialise" in the sense of providing an initial value other than garbage.

    Quote Originally Posted by Programmer_P
    Ok, so would something like this work?
    Yes (other than the global main function), but why is the member variable protected instead of private? You also do not need to explicitly define the destructor. You should use the initialisation list in the constructor, e.g.,
    Code:
    edit_menu_options_class::edit_menu_options_class() : undo("Undo") {}
    As for the global main function... it depends on what you are trying to do. Obviously attempting to print a variable that has not even been declared will not work.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    No, you can define them in other functions as well (including member functions), or if they are to have static storage duration, outside of functions too.
    Ok, cool! That's a relief.
    I think that instead of "define" you want to "initialise" in the sense of providing an initial value other than garbage.
    Well, whatever the "proper" term is, that's what I want...
    Yes (other than the global main function), but why is the member variable protected instead of private?
    I made it protected because I plan to have the stuff that option actually does in a derived class of edit_menu_options_class.
    You also do not need to explicitly define the destructor. You should use the initialisation list in the constructor, e.g.,
    Code:
    edit_menu_options_class::edit_menu_options_class() : undo("Undo") {}
    Ok, thanks! I will look into using inialisation lists instead then.
    As for the global main function... it depends on what you are trying to do. Obviously attempting to print a variable that has not even been declared will not work.
    Right, duh! I should have realized that. Of course I was just trying to get the general point, and what I had in mind, across...

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    I made it protected because I plan to have the stuff that option actually does in a derived class of edit_menu_options_class.
    That sounds like you are using public inheritance to reuse an implementation rather than to allow the derived class to be used by an existing implementation (i.e., to make use of polymorphism). It is not necessarily wrong, but it is not likely to be a good solution either.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question

    Quote Originally Posted by laserlight View Post
    That sounds like you are using public inheritance to reuse an implementation rather than to allow the derived class to be used by an existing implementation (i.e., to make use of polymorphism). It is not necessarily wrong, but it is not likely to be a good solution either.
    Huh? How did you draw that conclusion?
    If I understand inheritance right, the stuff that a parent class has will be passed on to the derived class, not the other way around...
    SO, in that sense, I would say I'm creating something in the parent class (i.e. edit_menu_options_class), namely an array called "undo", and I'm passing it on to the class that inherits it, which is what will actually perform actions with that thing. At least, that's the way I thought of it...

    And no, I haven't posted the code that will do that!
    Last edited by Programmer_P; 06-06-2009 at 01:03 AM.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    If I understand inheritance right, the stuff that a parent class has will be passed on to the derived class, not the other way around...
    Yes, but the key is in exactly what is the "stuff". Appropriate use of inheritance is typically tied to polymorphism, in which case the "stuff" that is "passed on" is the interface, not the implementation (or at least not only the implementation).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Yes, but the key is in exactly what is the "stuff". Appropriate use of inheritance is typically tied to polymorphism, in which case the "stuff" that is "passed on" is the interface, not the implementation (or at least not only the implementation).
    Well, I guess I need you to explain more clearly what you mean by "interface" and "implementation" (more so the latter than the former)...
    When I think of "implementation", I think of something being used, or "implemented" for whatever purpose. SO, in that sense, I really don't see what's wrong with implementing something in a derived class that exists in the parent class, and that's really what I'm doing anyway.
    In my mind, at least, the "undo" array/string will be declared in the parent class edit_menu_options_class, and then passed on to a derived class of that one, where it will actually be used, or "implemented".
    As for the interface, I would say "Undo" would qualify as part of the "interface" of my program, since its supposed to be a visible option in the Edit menu, and something the user interacts with.
    Last edited by Programmer_P; 06-06-2009 at 01:04 AM.

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Programmer_P View Post
    Because I'm lazy, and that stuff is complicated!
    And using arrays has made your life so much simpler ...

    Seriously, learn std::string. It takes about five minutes, and is far, far easier to use than raw arrays.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Well, I guess I need you to explain more clearly what you mean by "interface" and "implementation" (more so the latter than the former)...
    Refer to Stroustrup's glossary:
    interface - a set of declarations that defines how a part of a program can be accessed. The public members and the friends of a class defines that class' interface for other code to use. A class without data members defines a pure interface. The protected members provide an additional interface for use by members of derived classes. See also: abstract class.
    Consequently, "implementation" refers to the code that is behind the interface, i.e., what makes the interface concretely usable.

    Quote Originally Posted by Programmer_P
    When I think of "implementation", I think of something being used, or "implemented" for whatever purpose. SO, in that sense, I really don't see what's wrong with implementing something in a derived class that exists in the parent class, and that's really what I'm doing anyway.
    In my mind, at least, the "undo" array/string will be declared in the parent class edit_menu_options_class, and then passed on to a derived class of that one, where it will actually be used, or "implemented".
    To implement is not to use. (For example, did you implement the operating system that you are using?) To implement is to construct, to build, to turn the abstract into the concrete. Thus, you are not implementing the member variable in the derived class. You are directly making use of an inherited member variable.

    Quote Originally Posted by Programmer_P
    As for the interface, I would say "Undo" would qualify as part of the "interface" of my program, since its supposed to be a visible option in the Edit menu, and something the user interacts with.
    I am talking about the class' interface, not the user interface.

    Let me put it as a series of questions: what are the member functions of edit_menu_class and edit_menu_options_class? Which of these member functions are virtual functions? Are any of these virtual functions pure virtual? Does edit_menu_options_class have a virtual destructor?
    Last edited by laserlight; 06-06-2009 at 04:28 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Programmer_P View Post
    Because I'm lazy, and that stuff is complicated!
    If the time you spent trying to learn C++ by online conversation was spent in self-study with Bruce Eckel's free book and a good free compiler, you would be much further along.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM