Calling const-members

Discussion in 'C++' started by et al., Sep 6, 2010.

  1. et al.

    et al. Guest

    Hi again! I am still learning through examples, and still I am
    struggling with matrix classes! :)

    I have a probably naive question: how do I "force" C++ to use a const
    member? I mean, I have learned (through your suggestions) about const
    members, for example my at() member in the matrix class:

    class matrix
    {
    public:
    matrix(unsigned int rows, unsigned int cols);
    matrix(matrix const& src);

    virtual ~matrix();

    virtual double& at(unsigned int r, unsigned int c);
    virtual double at(unsigned int r, unsigned int c) const;
    // ...
    }


    Now, when I print my matrix using std::cout, the non-const member is
    called, while I was expecting otherwise! My logging member is nothing
    but a simple double loop:


    void matrix::log()
    {
    // ...
    for (i = 0; i < getRows(); i++)
    {
    for (j = 0; j < getColumns(); j++)
    cout << showpos << scientific << at(i, j) << " ";

    cout << endl;
    }
    }


    What am I missing here?

    Thanks!
    et al., Sep 6, 2010
    #1
    1. Advertising

  2. et al.

    gwowen Guest

    On Sep 6, 1:27 pm, et al. <

    If your log() function is not const, it will not call the const
    members. Try:

    void matrix::log() const;
    gwowen, Sep 6, 2010
    #2
    1. Advertising

  3. et al.

    Sensei Guest

    On 2010-09-06 14:39:56 +0200, gwowen <> said:

    > On Sep 6, 1:27 pm, et al. <
    >
    > If your log() function is not const, it will not call the const
    > members. Try:
    >
    > void matrix::log() const;



    You're totally right, that works!


    Thanks!


    --

    Sensei <Sensei's e-mail is at Me-dot-com>

    Research (n.): a discovery already published by a chinese guy one month
                   before you, copying a russian who did it in the 60s.
    Sensei, Sep 6, 2010
    #3
  4. gwowen <> wrote:
    > On Sep 6, 1:27 pm, et al. <
    >
    > If your log() function is not const, it will not call the const
    > members. Try:
    >
    > void matrix::log() const;


    If for whatever reason one would want to call the const version of
    a method from a non-const method (which cannot be made one), one could
    always cast 'this' to a const-pointer and call the method through that.

    I don't know if there's a "better" way of doing it other than that.

    //-------------------------------------------------------------------
    #include <iostream>

    class A
    {
    public:
    void function() { std::cout << "non-const function()\n"; }
    void function() const { std::cout << "const function()\n"; }

    void functionWhichMustBeNonConst()
    {
    function(); // call the non-const version
    static_cast<const A*>(this)->function(); // call the const version
    }
    };

    int main()
    {
    A a;
    a.functionWhichMustBeNonConst();
    }
    //-------------------------------------------------------------------
    Juha Nieminen, Sep 7, 2010
    #4
  5. On Sep 7, 9:27 pm, Juha Nieminen <> wrote:
    > //-------------------------------------------------------------------
    > #include <iostream>
    >
    > class A
    > {
    >  public:
    >     void function() { std::cout << "non-const function()\n"; }
    >     void function() const { std::cout << "const function()\n"; }
    >
    >     void functionWhichMustBeNonConst()
    >     {
    >         function(); // call the non-const version
    >         static_cast<const A*>(this)->function(); // call the const version


    When only changing the const-ness, I would advocate using a
    const_cast:
    const_cast<const A*>(this)->function(); // call the const version

    >     }
    >
    > };
    >


    Bart v Ingen Schenau
    Bart van Ingen Schenau, Sep 8, 2010
    #5
  6. Bart van Ingen Schenau wrote:
    > On Sep 7, 9:27 pm, Juha Nieminen <> wrote:
    >> //-------------------------------------------------------------------
    >> #include <iostream>
    >>
    >> class A
    >> {
    >> public:
    >> void function() { std::cout << "non-const function()\n"; }
    >> void function() const { std::cout << "const function()\n"; }
    >>
    >> void functionWhichMustBeNonConst()
    >> {
    >> function(); // call the non-const version
    >> static_cast<const A*>(this)->function(); // call the const version

    >
    > When only changing the const-ness, I would advocate using a
    > const_cast:
    > const_cast<const A*>(this)->function(); // call the const version


    I thought that is an indication of broken design
    Vladimir Jovic, Sep 8, 2010
    #6
  7. * Bart van Ingen Schenau, on 08.09.2010 09:22:
    > On Sep 7, 9:27 pm, Juha Nieminen<> wrote:
    >> //-------------------------------------------------------------------
    >> #include<iostream>
    >>
    >> class A
    >> {
    >> public:
    >> void function() { std::cout<< "non-const function()\n"; }
    >> void function() const { std::cout<< "const function()\n"; }
    >>
    >> void functionWhichMustBeNonConst()
    >> {
    >> function(); // call the non-const version
    >> static_cast<const A*>(this)->function(); // call the const version

    >
    > When only changing the const-ness, I would advocate using a
    > const_cast:
    > const_cast<const A*>(this)->function(); // call the const version
    >
    >> }
    >>
    >> };


    Someone once advocated not doing that, because it generates false positives when
    searching for bad casts. Instead, that person argued, one should simply declare
    a reference to self. Like

    A const& constSelf = *this;
    constSelf.function();


    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Sep 8, 2010
    #7
  8. et al.

    Richard Guest

    [Please do not mail me a copy of your followup]

    "Alf P. Steinbach /Usenet" <> spake the secret code
    <i682ag$4f7$-september.org> thusly:

    >> When only changing the const-ness, I would advocate using a
    >> const_cast:
    >> const_cast<const A*>(this)->function(); // call the const version
    >>
    >>> }
    >>>
    >>> };

    >
    >Someone once advocated not doing that, because it generates false
    >positives when
    >searching for bad casts.


    The assumption being that any const_cast<> is a "bad" cast?
    --
    "The Direct3D Graphics Pipeline" -- DirectX 9 draft available for download
    <http://legalizeadulthood.wordpress.com/the-direct3d-graphics-pipeline/>

    Legalize Adulthood! <http://legalizeadulthood.wordpress.com>
    Richard, Sep 8, 2010
    #8
  9. * Richard, on 08.09.2010 18:26:
    > [Please do not mail me a copy of your followup]
    >
    > "Alf P. Steinbach /Usenet"<> spake the secret code
    > <i682ag$4f7$-september.org> thusly:
    >
    >>> When only changing the const-ness, I would advocate using a
    >>> const_cast:
    >>> const_cast<const A*>(this)->function(); // call the const version
    >>>
    >>>> }
    >>>>
    >>>> };

    >>
    >> Someone once advocated not doing that, because it generates false
    >> positives when
    >> searching for bad casts.

    >
    > The assumption being that any const_cast<> is a "bad" cast?


    Presumably the assumption is that any const_cast that casts away const may be a
    bad cast, and that grepping for const_cast is easy while grepping for const_cast
    that casts away const is not practically doable.

    However, thinking about it there is also a readability argument for not using
    const_cast unnecessarily.

    Because the cast notation communicates that something is being forced, while
    adding constness is not something that needs to be forced.


    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Sep 8, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rakesh Sinha
    Replies:
    4
    Views:
    1,832
    Rakesh Sinha
    Jan 13, 2005
  2. Replies:
    11
    Views:
    1,082
  3. Javier
    Replies:
    2
    Views:
    533
    James Kanze
    Sep 4, 2007
  4. 0m
    Replies:
    26
    Views:
    1,080
    Tim Rentsch
    Nov 10, 2008
  5. Vladimir Menshakov
    Replies:
    1
    Views:
    350
Loading...

Share This Page