Class constructor with initialization

This is a discussion on Class constructor with initialization within the C++ Programming forums, part of the General Programming Boards category; I want to be able to declare a class instance and give it an initial value like this. MORSE instanceName ...

  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,139
    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, 04:01 PM
  3. Replies: 4
    Last Post: 12-12-2002, 01:32 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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