On declaring classes. (Newbie)

This is a discussion on On declaring classes. (Newbie) within the C++ Programming forums, part of the General Programming Boards category; Ah, nevermind. It had to do with the way I was doing the function. I'm not sure what I was ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    106
    Ah, nevermind. It had to do with the way I was doing the function. I'm not sure what I was doing but obviously it was wrong and I am a VERY BAD BOY.

    Anyway though, sort of derailing this, how do I compile/create a multi-source project, again?

  2. #17
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Mm, well, what compiler/IDE/whatever are you using? In MSVC you just add a new .cpp file to your project and a .h file to put your function prototypes in, include the .h file in main.cpp, and it will all be built together.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #18
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Dev-C++... Look a few posts back.

    Anyway, you can make projects in Dev, and do essentially the same thing as MSVC.
    Another way, compile (do not build) all of your source files individually, and then build the project (this will link the object files). I don't use Dev (or Windows for that matter) anymore, so I can't give you much more than that.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Hunter2
    >>What exactly IS the :member syntax after the function prototype anyway?
    It's called an "initializer list". The following is a simple example.

    No initializer list, simple assignment within constructor body:
    Code:
    class A
    {
    public:
        A(int i1, int i2, int i3)
        {
            a = i1;
            b = i2;
            c = i3;
    
        }
    
        int get_a()
        {
            return a;
        }
    
    private:
        int a;
        int b;
        int c;
    };
    Initializer list:
    Code:
    class A
    {
    public:
        A(int i1, int i2, int i3): a(i1), b(i2), c(i3)
        {
    
        }
    
        int get_a()
        {
            return a;
        }
    
    private:
        int a;
        int b;
        int c;
    };
    The code:

    : a(i1), b(i2), c(i3)

    says to set a equal to i1, set b equal to i2, and set c equal to i3.

    Initializer lists can be more efficient because they initialize the member as it's being created, rather than creating the member and then performing an assignment, and in some cases an initializer list is the only way to set values for certain types of members.
    Last edited by 7stud; 01-30-2005 at 11:25 PM.

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>No initializer list, simple assignment within constructor body:
    Yes, for POD types. But if you have a class with no copy constructor or = operator defined, or no default constructor either, then you MUST do the initialization in the initializer list.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    >>No initializer list, simple assignment within constructor body:
    Yes, for POD types. But if you have a class with no copy constructor or = operator defined, or no default constructor either, then you MUST do the initialization in the initializer list.
    See above:
    ...and in some cases an initializer list is the only way to set values for certain types of members.
    Does the code I posted produce errors? I wonder if you got half of your 2,000 posts with responses like that? In addition, you might want to try to learn to speak to your audience.
    Last edited by 7stud; 01-31-2005 at 03:00 PM.

  7. #22
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by Hunter2
    >>No initializer list, simple assignment within constructor body:
    Yes, for POD types. But if you have a class with no copy constructor or = operator defined, or no default constructor either, then you MUST do the initialization in the initializer list.
    Also, the initializer list is the proper place to call base class ctors.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    106
    Dev-C++ doesn't handle projects right for some reason. It doesn't recognize any changes I make to the included headers and whatnot. Yes, I AM saving them between compiles. I'm thinking it's not making new objects (Is that what you get from the first compile stage?) and just tossing in the old ones or something.

  9. #24
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>and in some cases an initializer list is the only way to set values for certain types of members.
    Didn't see that.

    >>Does the code I posted produce errors?
    No, but the object of my post was to point out that even though the results may be the same, using the initializer list is in no way equivalent to an assignment.

    >>I wonder if you got half of your 2,000 posts with responses like that?
    Practice what you preach. Your own post is little more than a restatement of my previous post, and less accurate and in-depth at that.

    >>In addition, you might want to try to learn to speak to your audience.
    My posts are for everybody to see; I simply added to (clarified?) your post. On the other hand, you've just contradicted yourself by dedicating an entire post to 'defending' yourself and insulting me, even though I did nothing deserving of your flames.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #25
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Hunter2
    >>Does the code I posted produce errors?
    No, but the object of my post was to point out that even though the results may be the same, using the initializer list is in no way equivalent to an assignment.
    Where did you say that? Here is your entire post:
    >>No initializer list, simple assignment within constructor body:
    Yes, for POD types. But if you have a class with no copy constructor or = operator defined, or no default constructor either, then you MUST do the initialization in the initializer list.
    You never even used the word "assignment". Once again, compare that to what I posted:
    Initializer lists can be more efficient because they initialize the member as it's being created, rather than creating the member and then performing an assignment, and in some cases an initializer list is the only way to set values for certain types of members.
    ====

    Quote Originally Posted by Hunter2
    Your own post is little more than a restatement of my previous post, and less accurate and in-depth at that.
    I didn't notice where you identified the code as an initializer list, nor explained in simple terms what it does. This is what I saw:
    The constructor of B takes an A*, and we'll name it 'parent'. Now, when B's constructor is called, i.e. it's being constructed, it will construct member_parent which is an A* by passing the A* constructor 'parent'. Since a pointer is a POD type, the 'constructor' of A* is automatically created, and all it does is copy the value passed to it into itself. So what happens is the value stored in 'parent' is copied into member_parent when B's constructor is called.
    ----
    Last edited by 7stud; 01-31-2005 at 04:52 PM.

  11. #26
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Quote Originally Posted by suzakugaiden
    Dev-C++ doesn't handle projects right for some reason. It doesn't recognize any changes I make to the included headers and whatnot. Yes, I AM saving them between compiles. I'm thinking it's not making new objects (Is that what you get from the first compile stage?) and just tossing in the old ones or something.
    If you are having trouble with porjects (from my past experience, it handles them well, but I don't have Windows to use it on, so I can't directly help you troubleshoot), then you can compile then individually. Simply compile -- but do not build -- all of your source files. Then build the project. Assuming they are all in the same directory, you should not have problems. If you think it is not properly recreating your object files, simply go into the folder, delete them, and then perform your compilations, making sure you are compiling the correct copy of the source files.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #27
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I didn't notice where you identified the code as an initializer list
    I did not identify it as an initializer list very simply because I did not know the correct terminology; I did not dispute that you added this information to the discussion. Because I realized that my explanation was somewhat confusing and difficult to follow, I added a code example that hopefully explained better; and above all, my information was accurate.

    >>rather than creating the member and then performing an assignment
    True, but the first 95% of your post seems to equate the initializer list with assignment, and only in the last line do you vaguely suggest that there is a difference - and then instead of stressing the greater significance of the distinction, all you say is that there is a performance gain due to the fact that the value is given during creation rather than the object being created and then assigned a value later!

    >>You never even used the word "assignment".
    I'd assumed that my comment would prompt further investigation into the matter, and the repeated use of 'constructor' would link back to my own explanation where I explained exactly what initializer lists do and how - and therefore the distinction would be made, albeit somewhat indirectly.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does declaring classes in the wrong order matter?
    By Swordsalot in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2005, 11:03 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21