Questions tagged [const-method]
23 questions
                    
                    123
                    
            votes
                
                11 answers
            
        Why is a public const method not called when the non-const one is private?
Consider this code:
struct A
{
    void foo() const
    {
        std::cout << "const" << std::endl;
    }
    private:
        void foo()
        {
            std::cout << "non - const" << std::endl;
        }
};
int main()
{
    A a;
   …
         
    
    
        Narek
        
- 38,779
- 79
- 233
- 389
                    105
                    
            votes
                
                5 answers
            
        C++ - Why static member function can't be created with 'const' qualifier
Today I got a problem. I am in the need of a static member function, const is not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how?
         
    
    
        prabhakaran
        
- 5,126
- 17
- 71
- 107
                    35
                    
            votes
                
                5 answers
            
        What can a 'const' method change?
C++ methods allow a const qualifier to indicate that the object is not changed by the method. But what does that mean? Eg. if the instance variables are pointers, does it mean that the pointers are not changed, or also that the memory to which they…
         
    
    
        Daniel
        
- 924
- 1
- 11
- 16
                    31
                    
            votes
                
                4 answers
            
        Calling a const function rather than its non-const version
I tried to wrap something similar to Qt's shared data pointers for my purposes, and upon testing I found out that when the const function should be called, its non-const version was chosen instead.
I'm compiling with C++0x options, and here is a…
         
    
    
        coyotte508
        
- 9,175
- 6
- 44
- 63
                    27
                    
            votes
                
                2 answers
            
        On a nonconst object, why won't C++ call the const version of a method with public-const and private-nonconst overloads?
class C
{
public:
    void foo() const {}
private:
    void foo() {}
};
int main()
{
    C c;
    c.foo();
}
MSVC 2013 doesn't like this:
> error C2248: 'C::foo' : cannot access private member declared in class 'C'
If I cast to a const reference,…
         
    
    
        japreiss
        
- 11,111
- 2
- 40
- 77
                    21
                    
            votes
                
                4 answers
            
        In c++, why does the compiler choose the non-const function when the const would work also?
For example, suppose I have a class:
class Foo
{
public:
    std::string& Name()
    {
        m_maybe_modified = true;
        return m_name;
    }
    const std::string& Name() const
    {
        return m_name;
    }
protected:
    std::string…
         
    
    
        Caleb Huitt - cjhuitt
        
- 14,785
- 3
- 42
- 49
                    13
                    
            votes
                
                13 answers
            
        Should I declare these methods const?
I'm working on some C++ code where I have several manager objects with private methods such as
void NotifyFooUpdated();
which call the OnFooUpdated() method on the listeners of this object.
Note that they don't modify the state of this object, so…
         
    
    
        starblue
        
- 55,348
- 14
- 97
- 151
                    8
                    
            votes
                
                1 answer
            
        How come a const temporary chooses to call a non-const member function over a const one?
The example code is taken from: http://en.cppreference.com/w/cpp/types/add_cv
(I modified a little.)
struct foo
{
    void m() { std::cout << "Non-cv\n"; }
    void m() const { std::cout << "Const\n"; }
};
template
void call_m()
{
 … 
         
    
    
        Frahm
        
- 805
- 2
- 9
- 16
                    5
                    
            votes
                
                1 answer
            
        Thread safety of const reference return of const method
Consider this class:
#include 
class A {
private:
    std::vector m_vector;
public:
    void insertElement(int i) {
        m_vector.push_back(i);
    }
    const std::vector& getVectorRef() const {
        return m_vector;
   …   
         
    
    
        Juergen
        
- 3,489
- 6
- 35
- 59
                    4
                    
            votes
                
                3 answers
            
        C++ overload resolution, conversion operators and const
In this case
void f(int *);
void f(const int *);
...
int i;
f(&i);
the situation is pretty clear - f(int *) gets called which seems right.
However, if I have this (it was done like that by mistake(*) ):
class aa
{
public:
    operator bool()…
         
    
    
        Goran Mitrovic
        
- 95
- 4
                    3
                    
            votes
                
                1 answer
            
        Why cannot a const qualified method be called on a non const object if a non const qualified private method exists?
The following code does not compile:
struct A {
    void f () const { }
private:
    void f () { }
};
int main () {
    A a_nc;
    const A a_c;
    a_nc.f();
    a_c.f();
    return 0;
}
The error:
test.cpp: In function 'int…
         
    
    
        Holt
        
- 36,600
- 7
- 92
- 139
                    2
                    
            votes
                
                2 answers
            
        How to use decrement operator on a static variable in a class
class AccountManager
{
private:
    Account accountlist[100];
    int *accountNumber;
    Account* SuperVipAccount; 
    static int ManagerNumber;
public
    int getManagerNumber() const;
};
I have a class like this, and I want to use decrement…
         
    
    
        lpy
        
- 587
- 1
- 6
- 20
                    2
                    
            votes
                
                5 answers
            
        const member functions can call const member functions only?
Do const member functions call only const member functions?
class Transmitter{
  const static string msg;
  mutable int size;
  public: 
    void xmit() const{
    size = compute();
    cout<
        
            
            
                
                    
    
    
         
    
    
                
            
        
     
 
    
    
        badmaash
        
- 4,775
- 7
- 46
- 61
                    2
                    
            votes
                
                2 answers
            
        Const method returning non-const reference to vector element
I'm having a hard time figuring out how to return a non-const reference to an element in a std::vector from a const class method. A simple example of what I'm going for is,
template
class MyClass
{
 public:
 MyClass : myVec(3)
 {
 }
 T&… 
         
    
    
        jlack
        
- 305
- 2
- 13
                    1
                    
            vote
                
                1 answer
            
        Can an object or its overloaded operator know if it's calling a `const` method?
struct X 
{ 
  void foo () {}
  void const_foo () const {}
};
struct Y
{
  X x;
  int i;
  X* operator-> () { return &x; } 
  const X* operator-> () const { return &x; }
};
int main ()
{
  Y y;
  y->foo(); // invokes `Y::operator->`
 …
         
    
    
        iammilind
        
- 68,093
- 33
- 169
- 336