C++ :: Access Private Data Of Base Class Without Access Modifier

Sep 9, 2013

if we don't provide the acces modifiers for base class and we need to manipulate the private data of base class in derived class. Is there anyway to acces the private data members? Here's a coding example

class A {
private :
int a;
};
class B : public class A {
public :
void displayA() { cout<<a<<endl; }
};

how i can acces the a of base class A in derived class B without acces modifiers.

View 16 Replies


ADVERTISEMENT

C++ :: Access To Private Member Map Outside Class

Oct 24, 2013

This question is more from a design point-of-view rather than coding it to be a fully functional.

So here it goes:

I have multiple files which each require their own object of same class type (ref. First Class). File contents are read from a file to a unordered_map<std::string, std::vector<std::string>> which is either private or protected member inside First Class. First Class does not need any public functions to add, remove or change the data during runtime, but changes are only being made by checking if the file size has changed during the day, if the size is not equal to the last check, map gets updated.

Now, I have a Second Class which is a data handler class. It has public member functions with arguments that needs to be get from First Class's unordered_map using const_iterator. Which way to go with design and implementation.

I know there's two methods to do this. Re-doing handler class is also not out of the question. These two methods I'm aware of are:

1. Declare these maps to local scope, build few global functions and here we go. (Probably the easiest way.)

2. Create public member functions to a First Class which either return a pointer or a reference to a protected/private member. (I'm under the impression that I really shouldn't be doing this because of a bad coding practice.)

Note that I don't need any code here, just some other point-of-views regarding the subject itself for learning better coding practices.

View 5 Replies View Related

C++ :: How Could Object Access Its Private Data Members From Outside

Nov 12, 2013

How does an object access its private data members in copy constructor.

The relevant part of the code: Code: C::C(const C &obj)
{
x = obj.x;
y = obj.y;
}

Normally the object1 called "obj" cannot access its private data members outside. But in this situation it can access. How can it be explained?

Here are the complete code:

Code:
#include <iostream>
using namespace std;
class C{
public:
C(int,int);
C(const C &);

[Code] .....

View 7 Replies View Related

C++ :: Cannot Access Private Member Declared In Class

Aug 22, 2013

I have a method:

int filetodb(std::wstring szwf, SQLHANDLESTR *h);

I want to use it, in a thread.

std says, use thread, as:

std::thread second (bar,0); // spawn new thread that calls bar(0)

How can I do this, for my method, that uses more than one, i.e., two, parameters?

My code is:

std::thread thread = std::thread(filetodb, filesP->at(i), h);

compiler says:

Error10error C2248: 'std::thread::thread' : cannot access private member declared in class 'std::thread'c:program files (x86)microsoft visual studio 11.0vcincludexmemory06061ConsoleApplicationa

How, can I do this?

View 2 Replies View Related

C++ :: How To Access Virtual Base Class

May 4, 2013

How can I access the virtual base class? This is a practice exercise from c++ primer plus 6.

The problem is that the name becomes No Name instead of the name specified when creating the gunslinger, I don't know how I can call the virtual base class explicitly

Output,

#ifndef PERSON_H_
#define PERSON_H_
#include <string>
#include <iostream>
#include <cstdlib>
using std::string;
class person

[code]....

View 7 Replies View Related

C++ :: Boost Threading - Cannot Access Private Member Declared In Class

Feb 25, 2013

When I put boost::thread Thread; in my struct I get the error error C2248: 'boost::thread::thread' : cannot access private member declared in class 'boost::thread'

The whole struct is

struct Player {
bool key[256];
char nameString[64];
bool lMouseButton;
bool rMouseButton;
double mouseX;
double mouseY;

[Code] ....

View 1 Replies View Related

C++ :: Cannot Access Private Member Declared In Class (header File)

Apr 1, 2014

I am currently doing the assignment about linked list. Here are some details information about what I am doing.. This program is C++ and should run on Visual Studio 2010. And it contains three file, two datastructure header and one main cpp file.

This program is trying to arrange and show some sports records. The main program which contain the functions such as reading the result text file(each result text file contain several records of athletes), removing a file, arranging the totalresult and printing it out. And the main program is already given and I cannot overwrite it.

But when I finished and try to build the solution and run it, I am not able to run the program and it give me somethings like these...

warning C4172: returning address of local variable or temporary
error C2248: 'Datastructure1::Datastructure1' : cannot access private member declared in class 'Datastructure1'
see declaration of 'Datastructure1::Datastructure1'
see declaration of 'Datastructure1'
This diagnostic occurred in the compiler generated function 'Result::Result(const Result &)'

And I have tried to comment each function part of the header file and see if can run or not. But I still fail to do so. Here are my codes...

#ifndef DATASTRUCTURE1_H
#define DATASTRUCTURE1_H
class Datastructure1 {
Public:
Datastructure1( );

[Code] ....

There are two header files and look quite long. They are all some linked list functions . I have read and learn linked list data structure before I complete this programs. However, when I complete the functions required, the function cannot be compile....

View 11 Replies View Related

C++ :: Unable To Access Private Variables Belonging To Object Class

Nov 21, 2013

I'm unable to access private variables belonging to the object class Date, which my overloaded >> operator is a friend of. I can't see anything in my code that would be causing this error. The .h file and the definition of the problematic overloaded operator from the implementation file are below:

#ifndef DATE_H
#define DATE_H
#include <string>
using namespace std;
class Date {
public:
// Initializes a date to the default value of January 1, 1970.

[Code] .....

The error message states that the vars (month, day, year) are declared as private in the header file and then a reference is made to the lines where I attempt to access these in the .cpp file and it reads: "in this context".

View 5 Replies View Related

C++ :: Cannot Access Private Member Declared In One Class But Can In Another Class

Sep 4, 2014

So I have an ImageManager class, Board class, and Box class. In Board.h I can declare ImageManager imgr; and in Board's constructor I can use imgr and its functions and such. However, in Box.h when I try and declare ImageManager imgr; I get the error "cannot access member declared in class ImageManager". Both declarations are under private, and exactly the same, but one doesn't work. Also, is there a way to only have one instance of ImageManager?

View 19 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++ :: Class Data Member Access?

Jul 5, 2013

I have some doubt regarding class data member accessing in another file.Follwing code showing error.

class A://file a.cpp
{
public:
int add;
int sub;
};
//file b.cpp
extern class A
void cal()
{
A::add=A::sub;
}

View 4 Replies View Related

C++ ::  Why Can't Get Access To Private Function

Aug 11, 2014

I've written a simple class given below. I had set the values through setab() function but the add()function didn't work.

Compiler shows CPP/class.cpp|20|error: ‘int Calc::add()’ is private|

#include <iostream>
using namespace std;
class Calc

[Code]....

View 5 Replies View Related

C++ :: Access Private Member Variable

Apr 3, 2013

I've created a class called Voter with a private member variable ID, also I have a variable in my main function to be ID as well. I'm trying to compare the two ID's but when I do so:

if (ID == V.ID)

I get the error - 'std::string Voter::ID' is private within this context.

I know that because it's private I can't access it, but how do I?

View 3 Replies View Related

C++ ::  Friend Function Can't Able To Access Private Member

Jan 21, 2015

Am trying to write table object into file. Here's the source code

.hpp file

class Table {
private:
int table_no;
std::string table_type;
bool engaged;
std::time_t start_time;
double total_sec;

[Code] ....

When i compile the above code i get the following error...

table.hpp: In function ‘std::ifstream& operator>>(std::ifstream&, Table&)’:
table.hpp:19:7: error: ‘int Table::table_no’ is private
table.cpp:91:12: error: within this context
table.hpp:20:15: error: ‘std::string Table::table_type’ is private
table.cpp:92:12: error: within this context ...........

View 4 Replies View Related

Visual C++ :: Constantly Calling Ostrstream - Private Member Access Violation

Sep 22, 2014

Code:
std::ostrstream oss;
oss << "path for " << unit << "
" << path;
puts(oss.str());

[Code] .....

Today, I just received this new fresh error, I was constantly using them, but just come to know it is a private access violation as the last error of my program. Did I use it in the wrong way?

View 3 Replies View Related

C++ :: How To Access Base Method Via Derived Pointer

Oct 1, 2013

I would like to access (use) a (virtual) method declared in a base class via a pointer to an object belonging to a derived class, which overrides the base method. Is it possible? Up to now I have not been successful. I am including a program with a more detailed description.

// declare a base class T0 with virtual function P, and a derived
// class T1 which overrides P; declare two ptrs, to T0 and T1,
// and generate corresponding objects; calling P via the two
// ptrs gives expected result; however I would like to take
// ptr to T1 object & execute the base function (declared
// in T0); is it possible? I tried all possible casts but nothing
// works; no matter how I cast the pt1 ptr, I always end up executing
// the overriding function (declared in T1)
// actual output (debian 7, gcc 4.7.2-5):

[code]....

View 5 Replies View Related

C++ :: Do Static Functions Have Access To Non Static Data Members Of A Class

Apr 17, 2013

From my book:

"A static function might have this prototype:

static void Afunction(int n);

A static function can be called in relation to a particular object by a statement such as the following:

aBox.Afunction(10);

The function has no access to the non-static members of aBox. The same function could also be called without reference to an object. In this case, the statement would be:

CBox::Afunction(10);

where CBox is the class name. Using the class name and the scope resolution operator tells the compiler to which class Afunction() belongs."

Why exactly cant Afunction access non-static members?

View 7 Replies View Related

C/C++ :: BST - Cannot Access Memory Data

Nov 11, 2014

When I return mem add I cannot access the data in that add: in bin search tree

Curr=findMin(root);
cout<<"val: "<<Curr->barcode<<endl;
bst::hwareItem* bst:: findMin(hwareItem*& root){

while(root!=NULL){
root=root->left;
}
return root;
}

View 1 Replies View Related

Visual C++ :: Access Variable Inside Class From Other Class

Nov 9, 2013

I am trying to access a variable from another class through another class but it is not returning its "instance"?

Example:
Class View

Code:
...
V3D_Viewer viewer;
...
Class MainWindow

Code:
...
viewer* myView;
myView = new viewer();
...
Class Test

Code:
...
MainWindow window;
window.myView->setBackgroundColor(WHITE);
...

I am new to c++ references and pointers,

View 3 Replies View Related

C++ :: SDL Library - Access To Pixel Data

Mar 7, 2013

I am using the SDL library, and came across a way to access pixel data.

//A pointer to a surface structure

SDL_Surface *w = SDL_SetVideoMode(800,400,8,SDL_SWSURFACE);

//Casting a void pointer pointing to an array of memory locations(the pixels), to a pointer to an array of Uint8's.
//SDL_MapRGB() Returns an Uint32 containing the color 0,0,255,(0/255, i think it returns the transparency too, or else it would rather return a Uint24,not sure).

Then modifying the memory location, in this case the pixel at the middle of the window.

static_cast<Uint8*>(w->pixels)[(200*w->pitch)+400]=SDL_MapRGB(w->format,0,0,255);

What i seem to not understand is that, i cast pixels to Uint8*, which means I now have a pointer to an array of 8bit numbers, but SDL_MapRGB seemingly returns an Uint32 or Uint24 for a color composed of r,g,b, together without 'a' is 24bits(256=1b,256=1b,256=1b)=3b.

And then i somehow assign that pixel which is of 8bit at chosen location, an Uint24 or 32bit unsigned int.

How does this work?

View 3 Replies View Related

C++ :: Access To Data Of Array While Have Its Name As String

Jul 31, 2013

The problem is as following. First, I have 21 arrays named: integer abee1, abee2, ... , abee20 and myarray all with the same dimension of [51][4]. Next, in a loop of 20 circles, I fill myarray with random integers then copy it into abee1-20 (each one per iteration). Then, for each array (abee1-20), I want to calculate sum of all numbers in second column as cost of that array and store it in the a new array called arrayCosts[20][2] ( char arrayName, int arrayCost)- the name of array in the arrayName column and cost of that array into arrayCost column . After that, I want to sort arrayName based on the cost and extract the 3 top array with lowest cost. I know that I should do a search in the array to find these three. Finally, copy these three into 3 new array called elite1, elite2, elite3.I do not know what to do.

1-The following code is for making the sting name of abee1-20 in string made in each step of the loop.

for ( cc = 1 ; cc < 20; cc++)
{ string String = static_cast<ostringstream*>( &( ostringstream () << cc) )->str();
string array_name = "abee" + String;

I want to use array_name (for example in the first loop which array_name is “abee1” ) for reaching the abee1 (which is a filled array) after dong a search in the array called “arrayCosts”. This search retrieves the name of array but it is string and I want to have the array with the same name.

View 2 Replies View Related

C++ :: Can't Access A Function In Other Class

Nov 24, 2014

I am essentially trying to get a value from a 'Matrix' that is created. Using a function in the class where the matrix is created. But when trying to do this I get the error: Call to non-static member function without an object argument in this line when trying to call the function:

Matrix::get(sizeR, sizeC);

View 6 Replies View Related

C++ :: How To Access Variable In Different Class

Oct 22, 2014

how can I make the queue of type pcb

and how to access pid in other clases?

View 6 Replies View Related

C/C++ :: How To Access Field Of A Class By Name

May 2, 2012

class myCls{
int x;
};

and then I create:

myCls c1;

and now:

is there way that I go like: cout << c1;
not overloading << operator;
and it shows me a value of c1.x?
just wanna replace c1 with that value;
I need sth similar to indexer from c#.

View 1 Replies View Related

C :: Gradebook Program - How To Store And Access Data

Jun 15, 2013

How I would store all the data. Also how would I access it after I store it. Side Note we are not allowed to use structures or objects.

For the purposes of this gradebook you should first provide a menu with the following options

-Add a new course Add a new student to a course Add grades for a student in a course Print a list of all grades for a student in a course Print a list of all students in a course Compute the average for a student in a course Compute the average for a course Store Gradebook (to a disk file) Load Gradebook (from a disk file)
-Each of these menu items should correspond to a function you will write.
-For the basic program each student will be represented by an ID number And each course by a course number
-Grades will be whole numbers only (no fractional part)

As indicated in the menu you will need to store and load using a disk file so that the data is retained.

Here are so limiting values in defining your data structures: Maximum Number of students (total) 100 Max number of courses 25 Max number of courses per student 4 Max number of grades per student per course 10

View 6 Replies View Related

C# :: Access Database Connection And Saving Data

Mar 17, 2014

I want to connect an access database (.mdb) to my Windows Forms application - this I can do and I have no problems viewing my table in server explorer.

Some code example or template that I can use as a base to learn from - I have literally trawled Google and Youtube and cannot find what I am after.

What I want to do is have a basic form, 2 text boxes and a button to save the data to my database.

FirstName and LastName are the 2 boxes.

View 6 Replies View Related







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