Solved

const members

Posted on 2004-10-30
236 Views
Last Modified: 2010-04-01
Could someone please explain to me how to create constant members. i tried initializing the constant in the constructor, but the compiler complains and requests that i initialize the member in the constructor base/member initializer list, what does this mean??

0
Question by:cpu4ghz
    2 Comments
     
    LVL 8

    Accepted Solution

    by:
    The member initialization list is the section in the constructor right after the colon (:).  It would look something like:

    // A.h
    class A
    {
    public:
      A();
    private:
      const x;
    };

    // A.cpp
    A::A()
    : x(321) // Initialize in the member initialization list
    {
    }

    Hope this helps.
    0
     

    Author Comment

    by:cpu4ghz
    Thanks alot for your help.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now