interesting indeed! (constructor question)

This is a discussion on interesting indeed! (constructor question) within the C++ Programming forums, part of the General Programming Boards category; hello again! Am I going crazy...how is it possible for a temporary class object to be created and assigned the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    interesting indeed! (constructor question)

    hello again!
    Am I going crazy...how is it possible for a temporary class object to be created and assigned the current value of the object as such:
    Code:
    Counter temp(*this);
    if you didn't declare a constructor that accepts a parameter to receive that argument?? The code works fine too, that's what's freaking me out. ANY insight into this would be awesome...perhaps the this pointer has special abilities I was unaware of...
    The only constructor I have is the default...

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    You also have a default copy constructor, assignment operator and destructor. You could do this if you wanted.
    Code:
    Counter temp(*this);
    ...
    *this = temp;
    Note also that temp in this case is simply a variable that you don't plan to use for very long, thats not a temporary. A temporary usually refers to an unnamed variable that the compiler creates and that often dies at the first ';'
    Code:
    std::ostream & center(const std::string &s, int cols = 80, std::ostream &os=std::cout) {
        int spaces = (cols-s.length())/2;
        if(spaces > 0) os << std::string(spaces,' ');
        return os << s;
    }
    ...
    center("Literal String");
    This creates a bunch of tempraries. A temporary std::string is created when we call center with the literal string, a temporary is created for cols-s.length(), and temporary is created when we create the string of spaces. The int "spaces" on the other hand, is just a variable I don't use for very long.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    If you inherit from the class type you're creating an object of, or the object you're doing the creating from is the same class type as the object you're creating, and assuming you haven't defined any constructors at all for that class, there will be (as grib said) a whole set of constructors and other stuff defined for you by default that will allow you to do what you just said.
    Just Google It. √

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A few interesting tools...
    By Mad_guy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-10-2009, 07:07 PM
  2. Interesting number theory problem
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-20-2003, 08:45 AM
  3. Interesting Registry Stuff (Keys, Values, etc.)
    By civix in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-27-2003, 09:58 AM
  4. An interesting analogous discovery...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 09-01-2002, 01:13 AM
  5. very interesting.....VERY interesting
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 08-16-2002, 11:02 PM

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