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


ADVERTISEMENT

C++ :: Store A Reference Variable As Member Variable Of Interface Object

May 1, 2013

I am having trouble compiling my interface. I am trying to store a reference variable as a member variable of the interface object. Compiler says that the variable has not be initiated correctly.

LCD inherits from VisualInterface which is expecting a DisplayDriver object to be passed in (DisplayDriver is another interface, but thats not important).

I pass the displayDriver object in when LCD is instantiated in maininterfaces.zip

I was pasing it before as a pointer but was told that this could cause me problems with memory leaks and a reference was better, but now I cant seem to get it to compile.

View 11 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++ :: Variable Declaration (const Pointer To SensorBase Object)?

Feb 19, 2015

I'm having to do a little c++ (coming from java) and don't understand the syntax of the following declaration

Code:
SensorBase* const sensor(mSensors[i]);

It looks like it's declaring a const pointer to a SensorBase object but I don't understand how that applies to sensor(mSensors[i]) which looks like a function??

View 6 Replies View Related

C++ :: Non-static Member Reference Must Be Made Relative To A Specific Object

Mar 4, 2012

Code:
class A
{
std::map<std::string, Unit*> aMap;
class B

[Code] .....

This code snippet results in "A non-static member reference must be made relative to a specific object". When I make callA() static, this error goes away, but there is problem with aMap.

View 2 Replies View Related

C++ :: Undefined Reference Error When Accessing Static Variable Inside Member Function

Feb 10, 2013

I am modifying a set of static variables inside of the class's member function. The static variables are private. An example of what I'm doing is as below,

utilities.h
-----------
class utilities {
private:
static int num_nodes;

public:
void parse_details(char* );

[Code] ....

I get a compilation error in the function void utilities::parse_details(char* filename)

which says: undefined reference to `utilities::num_nodes'

compiler: g++

View 2 Replies View Related

C++ :: Using Member Function Of A Class In Another Class And Relate It To Object In Int Main

Aug 21, 2013

I am writing a program which is using SDL library. I have two different classes which one of them is Timer Class and the other is EventHandling Class.

I need to use some member functions and variables of Timer in some Eventhandling Class member functions, Although I want to define an object of Timer in int main {} and relate it to its member function that has been used in Eventhandling member function in order that it becomes easier to handle it, I mean that I want to have for example two objects of timer and two objects of Eventhandling class for two different users.

I do not know how to relate an object of a class from int main{} to its member function which is being used in another class member function.

Lets have it as a sample code:

class Timer {
private:
int x;

public:
Timer();
get_X();
start_X();

[Code] ....

View 4 Replies View Related

C++ :: Pass Class Object By Reference

Jun 25, 2013

I am having trouble working with third party dll's, libs and header files. I am trying to call a function.here is the function that is suppose to be called.

bool COAuthSDK::GetRequestToken(CClientDetails &objClientDetails)

it has this info of what it needs :

Name IN/OUT Description
m_environment IN Optional. Possible values are SANDBOX (default) and LIVE.
m_strConsumerKey IN OAuth consumer key provided by E*TRADE
m_strConsumerSecret IN OAuth consumer secret provided by E*TRADE
m_strToken OUT Returned by the function if successful
m_strTokenSecret OUT Returned by the function if successful
m_strCallback IN Optional; default value is "oob"

here is the COAuthSDK header

#ifndef _OAUTHSDK_H_INCLUDED_
#define _OAUTHSDK_H_INCLUDED_
#include "ETCOMMONCommonDefs.h"
#include "ETCOMMONOAuthHelper.h"
using namespace std;

[code].....

when I try to build the function it says that its in the dll's so I know I have to call it.here is the link to the build site if needed URL....

View 1 Replies View Related

C++ :: Pass Class Object By Reference?

Mar 17, 2014

I'm trying to pass a class object by reference.

total = mathfunction(i);
}
double mathfunction(retirement& j)
{
double R = 0.00, m = 0.00, r = 0.00, t = 0.00, totaled = 0.00,
numerator = 0.00, denom = 0.00, temp = 0.00;

[Code]....

I only tried to pass by reference because I figured passing by value would be a larger pain in the neck.

View 1 Replies View Related

C++ :: How To Get Object Of Original Class From Function Of Other Class Where Other Class Object Is Member Of Original Class

Jan 21, 2013

The case is like

class B{
public:
somedata;
somefunction();
}
class A{
public:
data;
function();
}

in somefunction i want a pointer to current object of class A m new to c++

View 2 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++ :: Size Of Object If There Is No Data Member In Class

Aug 20, 2013

What is the size of object in c++ , if there is no data member in the class ?

View 3 Replies View Related

C++ :: Class Member Variable Initialization?

Dec 18, 2013

Is it possible to initialize class member variables in their definition statement instead of using a constructor?

Is the code bellow correct?

class rectangle
{
float a=0;
float b=0;
public:
string color="Red";
...
};

Which C++ Standard allows it?

View 2 Replies View Related

C++ :: What Can't Non-const Object Receive Temporary Object

Sep 11, 2014

a function returns a temporary object like

int myfun(){
int x = 0;
return x;
}

this function will return a temporary integer now void fun1(const int & num); this function can receive from myfun().BUT void fun2(int & num); this function cannot receive from myfun() Why is that, Moreover what is lifetime of a temporary object like one returned in myfun() ???

View 7 Replies View Related

C/C++ :: Pointing Variable To A Member Function Outside A Class

Apr 13, 2012

In C++, how do i call a method member of class A from a class B, using a pointer. By the way Class A and B are of different types.

I read that when a pointer is pointing to member function it can only point member functions within the class. But how can i point to a member function outside the class.?????

for example

class A {
public:
int add(int x) {
return x+x;

[Code] .....

View 1 Replies View Related

C/C++ :: Defining Class Counter - Member Variable Should Be Private

Oct 23, 2014

The question is: Define the class Counter. An instance of this class is used to count things, but the counter should never be less than 0 (non negative number). The member variable should be private. I realize what I'm suppose to be using but can't implement the member functions needed..

int main(){
int value;
cin >> value;
Counter myCounter(value);
for (int i = 1; i <= MAXLOOP; i++) {
myCounter.increment();

[Code] ....

View 3 Replies View Related

C++ :: Creating Priority Queue Of Objects Transaction As A Variable Of Store Class

Dec 4, 2013

I've got 2 classes, Store and Transaction and I would like to create a priority queue of objects Transaction as a variable of Store class.

My store.h
#ifndef __STORE_H_INCLUDED__
#define __STORE_H_INCLUDED__
#include <queue>
using namespace std;
#include "Transaction.h"
struct TransactionCompare;

[Code] ....

The error im getting with this set up is

error C2664: 'bool TransactionCompare::operator ()(const Transaction &,const Transaction &)
const' : cannot convert parameter 1 from 'Transaction *' to 'const Transaction &'

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++ :: Error - Invalid Initialization Of Non-const Reference Of Type

Feb 11, 2013

I am trying to use the Singleton design patterno on Linux, but it is not working:

in the .h I have:

Code:
public:
static ErrorH& Instance();
private:
ErrorH() {};

In the .cpp I have:

Code:
ErrorH& ErrorH::Instance() {
static ErrorH& self = ErrorH();
return self;
}

The errors I get are:

Code:
g++ --g -c ErrorH.cpp -o ErrorH.o
ErrorH.cpp: In static member function "static ErrorH& ErrorH::Instance()":
ErrorH.cpp:9: error: invalid initialization of non-const reference of type "ErrorH&" from a temporary of type "ErrorH"
make: *** [ErrorH.o] Error 1

This code works on Windows, how can I get it to work on Linux?

View 2 Replies View Related

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

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 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/C++ :: Passing Member Functions Member Variable To Another Variable

Aug 31, 2014

So I have a class object that contains the private member variable spot and the public member function MoveLock. Within MoveLock, is a member variable called numbers that holds the place where a user is on a "lock knob". Now, what I'm trying to accomplish is that whenever the user turns the "knob" in the wrong direction, the position is updated with that current numbers so that the clicks needed to unlock the first state is also updated. But I get these errors:

Error E2096 C:Users...switchtest.cpp 34: Illegal structure operation in function main()
Error E2294 C:Users...switchtest.cpp 39: Structure required on left side of . or .* in function main()

Ultimately, what I have in main() is a piece of what I'm going to implement in a class member function. I'm also thinking about moving the if else statements out of the for and creating a second one for the else portion.

#include <iostream>
#include <windows.h>
#include <iomanip>
using namespace std;
HANDLE inKeys = GetStdHandle(STD_INPUT_HANDLE);
HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

[code]....

View 10 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

C# :: Constructor Object Reference Not To Set Instance Of Object

Mar 28, 2014

I am trying to use web api in order to return custom json response keys. For this i have return a custom class to return json response

Custom Class:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

[Code].....

View 2 Replies View Related







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