rephrasing my question (clearer i hope).

This is a discussion on rephrasing my question (clearer i hope). within the C++ Programming forums, part of the General Programming Boards category; how do i get into a tempstring a string value from a data member of an object in an array ...

  1. #1
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    rephrasing my question (clearer i hope).

    how do i get into a tempstring a string value from a data member of an object in an array of objects?

    i thought it was:
    Code:
    strcpy(tempstring, array[index].getFunction() );
    where the function prototype is:
    Code:
    const char* getFunction() const;
    array[index] gives me the object i need to work with inside my array, while the getFunction() returns the data member value of that particular object, right?

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    What exactly doesn't work with the code? Wrong data or are you getting compile/link errors?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    ok, got it... this works... but what is the last const doing in a function prototype like this:

    Code:
    const char* getSomething(int index) const
    without that const it work like a charm, with it... i get:

    egg.cpp: In member function `const char* apples::getApples() const':
    egg.cpp:19: passing `const egg' as `this' argument of `const char*
    egg::getEgg()' discards qualifiers


    this is the full code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    class egg {
    	char eggs[10];
    	
    	public:
    		egg() {setEgg("eggs");}
    		void setEgg(char a[]) {strcpy(eggs,a);}
    		const char * getEgg() { return eggs;}
    };
    
    class apples {
    	egg apple[10];
    	int index;
    
    	public:
    		apples() {index = 0;}
    		const char * getApples() const { return apple[index].getEgg(); }
    };
    
    int main(){
    	egg theEgg;
    	apples theApple;
    	char i[10];
    
    	strcpy(i, theApple.getApples() );
    	printf("%s \n", i);
    
    	return 0;
    }

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    so why do you put the const in there if it works without it. AFAIK there shouldn't be anything between the
    Code:
    ()
    and
    Code:
    {}
    of a function definition or prototype.

    edit: sorry about the code bull, but for some reason it won't let me post this message without it.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    because the function prototypes given have that last "const" in there. So i'm just trying to figure out what it does. I found some examples in a book but i'm still clear as mud on this.

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Try taking it out of the prototype. I have no clue why it would be in there.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You add the "const" after a member function name if the member function does not logically change the state of the object or does not allow other code to change the state of the object.

    Since getEgg() just returns a const char *, it doesn't change the state of the egg object, and whoever gets that char * cannot modify it because it is const. Because of this, the egg::getEgg function should be const.

    Then, inside apples, the getApples() function actually is marked as const (referring to the const after the parentheses, not the const char*). This is logically correct, because it does not change its own state and only calls a function of one of its members that doesn't change the state of that member. The problem is that even though we know the getEgg() function is logically const, it doesn't say so in its declaration. You must add the "const" to the declaration of getEgg().

    It is a good idea to get in the habit of adding the "const" to member functions that don't change anything in the class. It tells the user of that class (which might be you a few months later), that the function won't change anything. It also allows for some performance improvements when the compiler knows that nothing will be changed. That is why I'd say it is a better fix to add the const to getEgg() than to remove it from getApples().

  8. #8
    rox
    rox is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    Great, now it makes sense. And it works too, but that comes as a bonus only

    Thank you very much!

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Good info, thanks for clearing that up. I'll probably start using that now too.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maths question ?????
    By ssharish2005 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-26-2008, 05:53 PM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Quick Question (I hope)
    By DocDroopy in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 08:35 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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