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


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

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++ :: Initializing Const Struct When Data Is A String Literal

Feb 23, 2015

I have a struct like this:

Code:
struct String{
char* data;
std::size_t size;
};

I would like to be able to create const variables of this type for string literals.

Code:
const String message;

Is there an elegant way to create a const String like this when data is a string literal?

I tried this:

Code:
const char *string_data = "Hello";
size_t string_size = strlen(string_data) + 1;
const String string = {string_data, string_size};

The problem with that is that string.data isn't considered const during the initialization of the String struct so the compiler throws an error. It doesn't feel very elegant to do it like this either way.

Is there an elegant solution to this problem? I would like to avoid making a copy of the string literal.

View 7 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++ :: Constructor Not Initializing Array?

Mar 6, 2015

I am making a tictactoe program that requires me to have a 3 by 3 two dimensional array of integers, in which the constructor should initialize the empty board to all zeroes. The program complies, but it keeps outputting garbage values and i'm not entirely sure why, My print function isn't complete yet, since I want to print out the array in a tic tac toe format, but i'm more concerned with why it's printing garbage values, here is my code:

Code:

// header file
#include <iostream>
#include <array>

[Code] ....

View 7 Replies View Related

C++ :: Initializing Object Through Constructor

Oct 15, 2013

sth is in my mind.

fast way of initializing an object through constructor is

Fred::Fred() : x_(whatever) { }

slow way
Fred::Fred() { x_ = whatever; }

ok. i accept it is faster but why?? what is the difference compiler does.

View 6 Replies View Related

C/C++ :: Initializing Constructor In CPropertyPage?

Aug 21, 2013

Here is a declaration of property class
class CMyPropertyPage11 : public CPropertyPage

Here is the definition
CMyPropertyPage11::CMyPropertyPage11() : CPropertyPage(CMyPropertyPage11::IDD)

I really do not understand the role of the
CPropertyPage(CMyPropertyPage11::IDD)

But I need to derive CMyPropertyPage11 from another CPropertyPage, say CBasePropertyPage adn really need to know how to implement the CPropertyPage(CMyPropertyPage11::IDD) initialization again.

View 3 Replies View Related

C++ :: Class Constructor With String Const Pointer As Parameter

Feb 19, 2013

I know my product.cpp constructor is wrong but how can I set my private variables.

//PRODUCT.h
#ifndef PROJECT1_PRODUCT_H
#define PROJECT1_PRODUCT_H

[Code].....

View 2 Replies View Related

C++ :: Initializing Map - No Matching Constructor For Initialization

Nov 18, 2014

I am trying to create a `std::map` from `std:: string` to a pointer to member function type. I'm initializing the `map` with an initializer list (braced). When I compile, I get a message from the compiler: No matching constructor for initialization.

Example: [URL] .....

View 4 Replies View Related

Visual C++ :: Initializing Array In Constructor?

Mar 8, 2015

I am making a tic tac toe program in which they are asking me to have a 3x3 2 dimensional array of integers and have the constructor initialize the empty board to all zeros. They also want me to place a 1 or 2 in each empty board space denoting the place where player 1 or player 2 would move The problem I'm having is, my code initializes the board to all zeros for each element of the array, and prints it out just fine, but I can't figure out how to re-initialize the values in the array to show where each player moves on the board... I was thinking about having a default constructor that has each value set to zero, and then use a setGame function that can change the values on the board to one or two depending on where the player moves....but I don't know if that's possible.....

here is my code

Code:

// header file
#include <iostream>
#include <array>
class tictac {
public:
tictac(int);

[code]....

View 8 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++ :: Calling Custom Constructor Of Element In Array Whose Class Has Const Members?

Apr 15, 2013

If I have an array of some class, and that class has const members, is there some way I can call a custom constructor on elements of the array?

I can't seem to reinitialize an element in foos in the example below. A thread on stack overflow mentioned the copy constructor show allow it, but I get "no match for call to '(Foo) (Foo&)'" when I try it.

Code:
class Foo {
public:
Foo();

Foo(int x, int y);

[Code] .....

View 4 Replies View Related

C++ :: How To Convert Char To Const Char

Jun 3, 2013

I have a file which contains a year and the name of an associated file to be read. I need to extract the data in the txt file and perform some calculations.

( year data file)
2004 2004data.txt
2005 2005data.txt
2006 2006data.txt

Here is what I do. I first declare "char yeardata" and then pass "2004data.txt" to it. Then I call yeardata in ifstream to extract the data inside the file "2004data.txt". The problem is that char yeardata is not constant so I cannot pass the file to it. It doesn't work if I change "char yeardata" to "const char yeardata".

Code:
int oldnewcomp_temp(char* lcfile) {
using namespace std;

int year;
char yeardata;

[Code] ....

View 12 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 Static Member Just Once Throughout

Jul 24, 2014

Here I'm trying to initialize PersonFactory::ethnicSurnames just once for the entire run of the program:

#include <iostream>
#include <string>
#include <vector>
#include <array>

enum Country {India, China, France, NumCountries}; // plus many other countries
struct School {}; struct Mall {}; struct HockeyArena {};

[Code] ....

Output:

PersonFactory::initializeEthnicNames() called
Carrying out the initialization...
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
numberOfTimesInitialized = 1

As you can see, even though five PersonFactory objects were constructed, the ethnicNames initialization only occurred once, as desired. However, there are some issues with my method. First of all, the use of the comma operator is ugly in my opinion. But fashion statements aside, PersonFactory::initializeEthnicNames() is still called multiple times, which is not good, even though it correctly avoids reinitializing ethnicNames after the first call. Also, I now forever get the annoying compiler warnings that the bool namesInitialized is never used, which is true, thus wasting a small bit of memory. And finally, I cannot declare ethnicNames const now, and it is supposed to be const. Any better way to accomplish what I'm trying to do?

By the way, the reason why I don't initialize ethnic names outside the class as is normally done for static data members (and that would indeed allow me to declare it const) is because it would get messed up if I later change the order of the elements in enum Country. Hence actual lines of initializations I think are needed. And I do want ethnicSurnames inside PersonFactory, because I feel it really does belong there. Also, PersonFactory is not to be Singleton, because it has data members that depend on some parameters in its constructor (e.g. geographic location).

View 5 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++ :: Initializing Enum Member In Constructer

Jul 25, 2012

I am learning OOP Console Programming and i got some error in my code;

Here is my code

#include <iostream>
using namespace std;
enum ch {CD,DVD}
class disk {
private :
ch choice;
[Code] ....

I want to compare the enum variable to char c, and cout CD when user press 'c' and cout DVD when user press 'd'. I got error like this :

error : expected ')' before 'c' at first line after public

Note : I must use enum to store value.

View 7 Replies View Related

C++ :: Initializing Base Class Part From Derived Object Using Copy Constructor

Feb 20, 2012

class Base
{
char * ptr;
public:
Base(){}
Base(char * str)

[code].....

Obj1 is a derived class object where base class char pointer is initialized with "singh" and derived class char pointer is initilized with "sunil". I want to create Obj2 out of Obj1. Separate memory should be created for Obj2 char pointer (base part and derived part as well) and that should be initialized with the strings contained in Obj1.

Here the problem is: Derived class part can be initialized with copy constructor. How to initialize the base class char poniter of Obj2 with the base class part of Obj1. char pointers in
both the classes are private.

I tried using initializer list but could not succeed.

Is there some proper way to do this?

View 4 Replies View Related

C++ :: Use Const Char As Buffer?

Feb 5, 2014

I want to use a const char* as a buffer. I am reading values from a file and adding them to a buffer. How to extract the values is simple enough. I am reading through a filestream, reading each character into a char pointer and progressing that char pointer every time. I have another char pointer marking the start positon

eg.

char *mychar = new char;
char *char1 = new char;
char *char2 = new char;
const char *constchar ;
char2 = char1;
while(filestream.read(mychar,1) {
*char1 = *mychar;
++char1;
}

Then I get this problem: constchar = mychar; // const char* = char*.

Constchar does not catch all the data in other words. At some stage some data is lost due to zeros in the data.. How can I put values into a const char and get around this problem? The const char* will //only record everything up until the first zero.

View 6 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++ :: String To Const Char Error

Jan 15, 2013

I'm currently finishing up an assignment that was half written by my professor. Below in the testGrades section of code there are two errors both are the same message.

Error: no matching function for call to Grades:: Grades(const char [15])

Test Grades

//Purpose: Test program for the class Grades
// Create stu1 Grades object
// Add 5 grades to stu1 - only 3 can be stored in stu1 - other 2 discarded
// Create stu2 Grades object
// Add only 2 grades

#include <iostream>
#include <string>
#include <iomanip>
#include "Grades.h"

using namespace std;

[Code] .....

View 5 Replies View Related

C++ :: Convert Int To Const Char (for A File Name)

Jan 20, 2014

i found a lot about how to convert int to const char * but non of them explain correctly or sometimes dont even work.

Here is my code:

#include <stdio.h>
#include <cstdlib>
using namespace std;
int main () {
int i =0;
char c1 =0;
char c2 =0;

[code]....

View 15 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++ :: Wild Output From Const Char Array

Nov 20, 2014

I have data that is coming into my buffer via popen (process data, not a file). Every seven records is a new set [0-6]. I am trying to 'print out the array line/element value' and 'change the value of element [2] to 0', but my loop appears to be looping through every character and not just every line?

code:

char* Data(){
char buff[BUFSIZ];
FILE *fp = popen("php order.php 155", "r");
std::string::size_type sz;
while(fgets(buff, sizeof buff, fp) != NULL)
}

[code]....

View 2 Replies View Related







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