Thread: Class constructor with initialization

  1. #1
    Registered User WebSnozz's Avatar
    Join Date
    Oct 2001
    Posts
    102

    Class constructor with initialization

    I want to be able to declare a class instance and give it an initial value like this.

    MORSE instanceName = 'A';

    I know I could do it with just passing 'A' as a parmeter but I'd like to make it look more like the standard data types.

    I'm thinking that

    MORSE instanceName

    does it's stuff with the default constructor and then the equal sign evaluates, if I'm guessing right then all I need is to overload the = operator so it can take a char as the right operand and a MORSE as the left operand.

    What's the syntax for the implementation of the equal sign if I needed to assign the right operand to a private variable called letter

    Should it be a member function or friend function?
    Does the equal sign return a value?


    Thanks in advance for replies.
    WebSnozz-
    Cats have no butt cheeks.
    If one farted, then it would make a flute noise.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    public member function:
    const MORSE& operator = ( char c );

    Implementation:

    Code:
    const MORSE& MORSE :: operator = ( char c )
    {
        this->letter = c;
        return *this;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Replies: 4
    Last Post: 12-12-2002, 02:32 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM