Thread: Can a struct have a constructor?

  1. #1
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Can a struct have a constructor?

    While I'm toying with the language this weekend, is there anyway to do this:

    Code:
    LearnTrick("Roll Over", 10, 10);
    or this
    Code:
    LearnTrick(Trick("Roll Over", 10, 10));
    if the function is prototyped as

    Code:
    LearnTrick(Trick);
    and a Trick is:

    Code:
               
       struct Trick {
               
          string name;
          int fatigue;
          int hunger;
       };

    I know I could use a class with a constructor for this, but is there any way to have a struct be pieced together in a function argument?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    A struct can have a constructor as well, much like a class. A struct is simply a class that has membership default to public rather than private in C++.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    LearnTrick(Trick("Roll Over", 10, 10));
    This won't do. The object Trick has a lifetime scope of that function call! And besides that, who is calling LearnTrick? Trick itself??

    Use them separately. If you need Trick to be declared in a larger scope but cannot teach it something just yet, declare Trick, and in main(), call Trick.LearnTrick("Roll Over", 10, 10));
    Otherwise, if you are declaring and teaching at the same time, do Trick("Roll Over", 10, 10);
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    No LearnTrick is a member function of a critter. It learns a Trick, which is added to a private array of tricks it has. Trick is a struct that contains a few ints and a string.

    Sil, so Trick::Trick() will work?

    cool.

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    It worked!

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. struct constructor woes
    By Mr_Jack in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2004, 03:36 PM