C++ :: When To Declare A Member Function As (const)

Sep 27, 2014

i am trying to describe the unusual situation where you declare a class member function with this format:

bool class::function_name(void) const

Specifically where the 'const' follows the parameter list. It is my understanding this is a very useful way of ensuring that whatever code you put in the function definition cannot change any data members of its class.

However I have recently read that this form of declaration should not be used as it leads to less optimized and slower code. Is this correct?

View 3 Replies


ADVERTISEMENT

C++ :: Initializing Const Data Member

Apr 11, 2014

What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.

class Book
{
friend void CompPrice(Book &,Book&); //friend function that has access to the member functions of this class
//The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions
private: //private data members

[Code].....

View 2 Replies View Related

Visual C++ :: Const Member Functions

Jan 24, 2013

Code:
class Editor {
// c'tors etc
Gtk::EventBox canvas_event_box;
void functionA();
void functionB() const;

[Code] ....

When I try to compile functionB in Visual C++ it gives me this error:-

glibmm/refptr.h(199) : error C2440: 'initializing' : cannot convert from 'const Gdk::Window *' to 'Gdk::Window *'
Conversion loses qualifiers

And this is the code from glibmm/refptr.h

Code:
// The templated ctor allows copy construction from any object that's castable. Thus, it does downcasts:
// base_ref = derived_ref
template <class T_CppObject>
template <class T_CastFrom>

[Code] .....

I don't actually want to change anything in the member variable canvas_event_box. I just want to be able to call one of its functions from my 'const' member function. Is there any syntax I can use to tell VC++ that I'm not actually changing the variable - just using it.

View 11 Replies View Related

C++ :: Typedef Template With Static Const Member?

Apr 18, 2013

class Tracker {
public:
static const int type;
typedef cv_types::CvType<type>::type_t type_t;
};
const int Tracker::type = 1;

gives me the error:

'I' : invalid template argument for 'cv_types::CvType', expected compile-time constant expression

Shouldn't the static const int be a compile time constant?

How would I specify it, so that it works?

PS.: The code works with #define type 1 at the top of the file and without the static const int.

View 3 Replies View Related

C++ ::  Initializing Const Char Member Variable In Constructor?

Jan 23, 2015

I have a class that defines a window (a popup dialog of sorts), and I want the name of that window to be constant. The only problem is that the name of the popup needs to match the title of the parent window, and I get the name of the parent in the constructor. So how do I go about defining this member variable to be constant and initializing it with a value in the constructor?

I want to do something like this, but I know this isn't allowed:

/* class.h */
class foo {
public:
foo(*parentWindowPtr);

[Code] .....

I should mention that yes the name of the parent window is const char *, and I would like to keep it this way.

View 4 Replies View Related

C++ :: Smarter Way To Get Value Of Member As To Declare New Parameter

Aug 20, 2013

Theres a class named "A" which has got a static function named "sfA".Now I instance an object of class A and call a method from A called "fA".

The method fA calls sfA. And now the issue is: i need the value of a member from the object which called fA respectivly sfA, inside sfA.Is there a smarter way to get the value of the member as to declare an new parameter for the sfA? sfA has to be static.

View 4 Replies View Related

C++ :: Declare Class Member Outside Of Constructor?

Jun 27, 2013

Basically, I need to set a variable outside of the constructor and make it accessible to the entire class.

It would need to work something like this:

#include <iostream>
#include <string>
template <typename MT> class CallbackFunction
{

[Code].....

View 5 Replies View Related

C++ :: Store Reference To Const Object In Class As A Member Variable?

May 27, 2014

i want to store reference to a const object in my class as a member variable, as follow:

I basically want a readonly reference to |Data| in Device object.

Code:

class Device {
Device(const QList<QSharedPointer<Data>> & dataList) : _listRef(dataList) {
} protected:
const QList<QSharedPointer<Data>> & _listRef;
}

This does not allow me to initialize _listRef as something like NULL when it is not applicable.Also, i must change all my constructors and its child class to include an initialization of _listRef!!

What is the alternative? Is pointer the nearest? which of the following should be used?

Code:
const QList<QSharedPointer<Data>> * _listRef;
or
const QList<QSharedPointer<Data>> *const _listRef;
or
const QSharedPointer<QList<QSharedPointer<Data>>> _listRef; ????

View 7 Replies View Related

C++ :: Const Method Accessing Private Data Member Giving Access Violations

Oct 17, 2014

My code is here [URL]

void Player::Display() const
{
cout << "
Player Name: " << GetName() <<
"
Grade: " << GetGrade() << "
G.P.A. " << GetGPA() << endl;
}

The problem occurs in here, I get access violations, is there a way to this while keeping Display const or is this code valid and my problem is somewhere else and not being caught in the debugger? I tried to make the return types const - but that didn't work .....

//Getters need const twice for this to work?
const char* Player::GetName() const {return m_name;}
const int Player::GetGrade() const {return m_grade;}
const double Player::GetGPA() const {return m_gpa;}

[Code].....

View 2 Replies View Related

Visual C++ :: Access Member Function From Non-member Function In Same CPP File?

Dec 16, 2012

In my MFC, CMyPorpertyPageDlg is derived from CPropertyPage. How to access its member function from a nonmember function in the same CPP file?.

void Non_Member_Get_PorpertyPage()
{
CMyPorpertyPageDlg* pPageDlg = ....
}

View 4 Replies View Related

C++ :: Call To Member Function X Is Ambiguous - Overloaded Member From Header File

Feb 23, 2014

I get the following error in XCode whenever I try to access the member I created 'randomGen' in a separate class in a different header file. I have made sure to include the header file and have tried to access it through an object.

This is the code I enter when trying to access the method from randomiser.h in main.cpp. It is also an overloaded function with doubles and integers:

RandomG randomiser;
randomiser.randomGen(); // 'Call to member function 'randomGen' is ambiguous'

This is the code inside randomiser.h:

#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class RandomG {

[Code] ....

This is the error inside xcode: [URL] ....

I have tried seperating the code for the functions in another class (main.cpp) and then running and it seems to works, so I'm not sure why I can't put everything in the .h file and then access it?

I would like it in a seperate file so it doesn't clutter my main. I am writing a game with SDL so that might be confusing and I would like the window to have a random title and other random properties, so it would be easier to use a function.

View 3 Replies View Related

C++ :: Call Member Function Inside Another Member Function?

Mar 21, 2013

If I wanted to call a member function inside another member function, how would I do that, if it's possible?

For example, if I have Find(int key) defined already and wanted to call it while i was overloading operator+.

View 2 Replies View Related

C :: Getting Const Char Array From Function

Jun 7, 2013

Now I am stuck with getting const char* array from function to main.

Code:

const char* values[3];
strings_to_array()
printf("%s
", values[1]);
printf("%s
", values[2]); function: Code:
const char* strings_to_array()
}

[code]....

First, I cant get strings printed out in main.

Second, is here way to get number of such elements which array contains like higher languages have "count" or "ubound" or such?

Third, next function which need that array assumes that array of const chars* is 1 based. Can that be set in C or here are allways zero based arrays?

View 1 Replies View Related

C++ :: Map Comparison Fails In Function Const

Jan 27, 2012

Why I'm getting an error here. I've dumbed down my code for simplicity and removed irrelevant code.

PHP Code:
class Foo {
    public:
        bool IsNull() const;
    private:
        std::map<int, int*> test;

[Code] ....

I'm getting a "passing...discards qualifiers" error on my if statement and not sure why because I'm not changing anything. I know removing const or making test mutable fixes the issue. I've been taught to always make a function const if it doesn't change anything, in which case, have I finally come across an acceptable time to use mutable?

View 9 Replies View Related

C++ :: Calling Const / Non-const Overrides

Oct 5, 2013

Are there other ways of calling a const/non-const override? I want to defined some functions in terms of others, particularly accessors which might or might not require constness- in order to not copy & paste code. This is my current solution:

struct dumbArray {
dumbArray(unsigned int size):
m_array(new int[size]){
}
~dumbArray(){
delete m_array;

[Code] .....

View 10 Replies View Related

C++ :: Difference Between Const And Static Const

Dec 7, 2013

difference between const and static const, more effectively. I know the basic concept of const and static but I need clear explanation of declaring "const" and "static const"

Ex:

const int a;
static const int a;

View 5 Replies View Related

C++ :: Casting Non-const Variable To Const

Jun 19, 2013

Is there any way to cast a non-const variable to const one?

I want to read variable n from file and then use it to declare array "int arr[n]", but because n is non-const, the compiler doesn't allow me to do that.

View 6 Replies View Related

C/C++ :: Const Pointer Pass By Reference In Print Function

Apr 21, 2014

I am trying use a print function to print out data in a struct. My questions are:

1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?

2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Inventory {

[Code] .....

View 9 Replies View Related

C/C++ :: How To Declare A Function For Summary Report

Oct 12, 2014

#include <iostream>
#include <cmath>
using namespace std;
void summary();
const float below100 = 0.10;
const float after100 = 0.05;

[Code] .....

It cannot loop back to the top coding, cause what i want is the code can loop back to the 1st cout question, and it will asking over and over again, and how to declare a function for the summary report?

View 2 Replies View Related

C :: Declare A Struct Then Pass It As A Parameter Into Function

Dec 8, 2014

I have some code here where I try to declare a struct then pass it as a parameter into a function to do something to it:

Code:
struct _user {
char * initial[3];
int pos;
} user;
int initial_add (struct user * initial_list, int initials, char * buffer) {

[Code] ...

I get the error :
server2.c:15: warning: "struct user" declared inside parameter list
server2.c:15: warning: its scope is only this definition or declaration, which is probably not what you want

[Code] ....

View 6 Replies View Related

C/C++ :: Main Function Can Only Declare Variables And Functions

Mar 9, 2015

I need making my main function to run while not having any if or for statements. It can only declare variables and functions. Since my main function has command line arguments, how to so.

// This program counts all the words in a given file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
FILE* txtFile = NULL; // File Pointer
char str[1000000];

[Code] ....

View 5 Replies View Related

C++ :: Non Constant Member Function Being Called Inside Constant Member Function?

Dec 28, 2012

#include <iostream>
class Hello {
public:
void Test() {

[Code].....

As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .

View 5 Replies View Related

Visual C++ :: How To Assign Const Char Return Value Of A Function To Label In Windows Form

Sep 30, 2013

I am a newbie to C++ and VS ++. I have created a windows form application by dragging and dropping button, label..etc. i wish label text to be appeared as return value from a function. The function returns ' const char* '.how this returned string pointer can be used to display label text.?

View 9 Replies View Related

C :: How To Make Main Function Only Allowed To Declare Variables And Functions

Mar 6, 2015

So I need to make a main function have no if/for/etc. statements so I need to move it to another function and call it in main. The problem is that it's a command line argument function so I'm confused on how it works. Here's an example:

Code:

#include <stdio.h>
int main(int argc, char* argv[])
{
printf("The program name %s", argv[0]);
if (argc == 2) {
printf("Argument supplied is %s", argv[1]); }
else if (argc > 2) {
printf("Too many arguments");}
else {
printf("One argument");}
}

How can i make this into two functions with main only declaring variables and calling other functions?

View 2 Replies View Related

C :: Functions That Declare Arrays In Function Input Even Though Already Declared In Main

Mar 6, 2015

I have seen functions that declare arrays in the function input even thou the arrays is already declared in main. Why do you do this?

For example:

int ova(int antal, char glosorSv[][MAX], char glosorEn[][MAX])
int main(void)

char glosorSv[][MAX]
char glosorEn[][MAX]

View 2 Replies View Related

C++ :: The Difference Between Const And Non Const Key

Jul 13, 2013

So I start thinking about what's the difference between this 2 code

map<const int, float> map_data;

map<int, float> map_data;

But it seems I can't find the difference, is there any difference between this 2 code ?

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved