IS this allowed in C++

This is a discussion on IS this allowed in C++ within the C++ Programming forums, part of the General Programming Boards category; Is the following acceptable .. Code: #include <time.h> #include <iostream> class Date { public: Date(Date& d); / / THIS ** ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    IS this allowed in C++

    Is the following acceptable ..

    Code:
    #include <time.h>
    #include <iostream>
    
    class Date						
    {
    public:
    
    	Date(Date& d); // THIS **
    
    	// Give access to the object
    	Date getDate() { return date; } // This is the reason I'm having the above
    
    	void SetDate(tm curr_tm);
    	static tm getCurrentTime();
    	int getDay(){return day;}
    	int getMonth(){return month;}
    	int getYear(){return year;}
    	int getHour(){return hour;}
    	int getMinute(){return minute;}
    	tm getLocalDT(){return local_dt;}
    	friend std::istream& operator>>(std::istream& in, Date& date);
    	friend std::ostream& operator<<(std::ostream& out, const Date& date);
    	
    private:
    
    	int day;
    	int month;
    	int year;
    	int hour;
    	int minute;
    	tm local_dt;
    
    	Date &date; // THIS **
    };
    
    
    #endif;

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Looks a bit fishy. What could possibly be the purpose of the date member?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    It doesn't look valid since you'll always need to have a valid instance of Date (or one of it's derived class) to initialize another instance. Which get to the question, how do you create the first instance of Date.

    What you might want is to make getDate a static function. It would make more sense... I think. But then, what is getDate supposed to do ? And... what is this class supposed to be exactly ? The interface looks rather confused.
    I hate real numbers.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by anon View Post
    Looks a bit fishy. What could possibly be the purpose of the date member?
    I need to get the date (Day, Month, Year, Hour, Minute, Second) as one object ... There's a Class which has Date as a member .. need to initialize that member

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by foxman View Post
    what is this class supposed to be exactly ? The interface looks rather confused.
    Date class like any other Date class .. What u mean is confused?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You basically have a chicken and the egg problem. In order to create a Date object you need to have an instance to an already existing Date object in which to assign the reference. However, in order to get that instance it would require an existing Date object in which to assign the reference. And the cycle continues.

    I don't know if the compiler will spit out an error or not as it doesn't seem to be any syntax error, just a logic error.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by csonx_p View Post
    Date class like any other Date class .. What u mean is confused?
    I can understand the int members.

    Then, in addition you have a tm member. What is that supposed to do, represent another date?

    And finally, what is the purpose of the Date reference?

    How many dates is one Date instance supposed to store?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by anon View Post
    Then, in addition you have a tm member. What is that supposed to do, represent another date?
    Yes, should this be a free function, or a friend maybe?

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Looks fine as long as you add another constructor. Perhaps a default constructor that just initializes date to "*this". Though I'm not clear on what the purpose of getDate() is.
    Last edited by King Mir; 08-30-2008 at 02:52 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    The only way to solve the chicken and egg problem caused by having a reference to the same type within the class (that I can see anyway) would be to have the constructor initialise it to *this. That would kind of defeat the purpose of having the reference since you could just use the 'this' pointer anyway.
    I therefore conclude that it is an error to have a member of a class that is a reference to an object of that same class.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,002
    Aside from that, you seem to having problems choosing a style?

    Date(Date& d); // THIS **
    friend std::istream& operator>>(std::istream& in, Date& date);
    friend std::ostream& operator<<(std::ostream& out, const Date& date);

    Date &date; // THIS **

    First it's Date& date, then it's Date &date.
    A copy-n-paste error? An overlooked detail?
    You should stick to one. And you've demonstrated you've used the former.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,623
    Quote Originally Posted by Thantos View Post
    You basically have a chicken and the egg problem. In order to create a Date object you need to have an instance to an already existing Date object in which to assign the reference. However, in order to get that instance it would require an existing Date object in which to assign the reference.
    Not true. While I don't advocate the technique, a class X can contain a reference to X without relying on another instance existing. For example;
    Code:
    class X
    {
           public:
    
               X & const self;
    
               X() : self(*this) {};
    };
    I've seen this sort of thing used by people who have migrated from other OO languages (smalltalk?) to C++, and are having trouble getting used to typing "*this" to say "self".

    The problem with this is that it requires all constructors (copy constructors, etc) to be manually defined for every class ..... there can be absolutely no reliance on compiler-generated defaults.

    Yes, I realise the problem in this thread is that a reference member is probably being used inappropriately. The above is noted in interests of completeness.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    Date getDate() { return date; } // This is the reason I'm having the above
    Why can't getDate do?
    Code:
    Date getDate() { return *this; } // This is the reason I'm having the above

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    Date getDate() { return *this; } // This is the reason I'm having the above
    If this is really what the OP want, it would be quite ridiculous. That would be like

    Code:
    int getInt(int n)
    {
       return n;
    }
    
    int main()
    {
       // a = 5 + 6
       int a = getInt(5) + getInt(6);
    }
    ...and I think we all agree that's useless, don't we ?
    I hate real numbers.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by foxman View Post
    Code:
    Date getDate() { return *this; } // This is the reason I'm having the above
    If this is really what the OP want, it would be quite ridiculous. That would be like

    Code:
    int getInt(int n)
    {
       return n;
    }
    
    int main()
    {
       // a = 5 + 6
       int a = getInt(5) + getInt(6);
    }
    ...and I think we all agree that's useless, don't we ?

    And of course, the current convoluted way of doing the same thing is better?

    In your example, it would be (somewhat simplified):
    Code:
    int getInt(int n)
    {
       int &nn = n;
    
       return nn;
    }
    Edit: The above is obviously somewhat "Rube Goldberg"-esque. It serves no purpose, but it will work from a logical perspective. I could not think of the name "Rube Goldberg" when I wrote my post, but it was what I had in mind.

    --
    Mats
    Last edited by matsp; 08-31-2008 at 06:33 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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

Similar Threads

  1. constructors not allowed a return type
    By kotoko in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2009, 11:51 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Java: Why isn't this allowed?
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-14-2005, 04:28 PM
  5. Constructors now allowed a return type: Mystery Error
    By wbeasl in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 02:33 PM

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