Thread: To Call an Object Outside of Class

  1. #16
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    To make your function work using const references the function CDate::convert() has to be const ( as it should to be const correct )

    e.g.
    Code:
    int CDate::convert() const {
        ....
    }
    Kurt

  2. #17
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Perhaps the attached code files will help...

  3. #18
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The class should be declared like this to be const correct .
    Code:
    class CDate
    {
    public:
    	CDate();
    	int setMonth();
    	int setDay();
    	int setYear();
    	int dayOfWeek() const;
    	int convert() const;
    	int nextDay()const;
    
    	bool isLeapYear() const;
    	bool isWeekDay() const;
    
    	void nameOfDay() const;
    
    	void showDate() const;
    
    
    private:
    	int m_month,
    		m_day,
    		m_year;
    	int m_convert;
    
    };
    Kurt

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, your problem lies in that the convert function is not const. However, convert also modified class data, so it cannot be const. Thus you have a little problem.
    I believe you should look over your implentation. Find a way to make convert const (ie make sure it doesn't modify class data).
    Otherwise you can't pass your arguments by const. And if you can't, then that means the function that takes those values can modify them, even though it shouldn't.

    Look over your implentation. Learn what a class is and what it should do. A class is an object - like a car. It hides its implentation to the outside and provides access to its functions. But I don't think I can agree that it should store a converted result of its date when you call convert.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  2. Is there a way to tell what class an object is?
    By Loduwijk in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 09:20 PM
  3. Linked list of a class object?....
    By chadsxe in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2005, 03:15 PM
  4. array of class object
    By TomButcher in forum C++ Programming
    Replies: 5
    Last Post: 09-03-2005, 09:48 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM