C/C++ :: Data In Private Class
Dec 11, 2014
i have private data look like Peiceorder peiceOrders[20]; it looks like a array but does not have name type like int or char? why is that and what should I use the data should I write Peiceorder peiceOrders[20]=........ like that? or?
View 1 Replies
ADVERTISEMENT
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
View Related
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
Feb 26, 2012
Basically, I've got one object which has to access private data in another object... and can't.
Here's the specifics: I'm writing a little war game program where players deploy units (soldiers, tanks, planes, etc.) onto a gameboard. Players and Units are modeled as objects:
Code:
class GameUnit {
public:
string GetName() {return Name;}
protected:
string Name;
};
class Player {
[Code] ....
Here's the problem: In the above code, Player's ListUnits() function doesn't work because Player can't access GameUnit's GetName() function.
Specifically, here's the compiler's error message:
Code:
In file included from Main.cpp:18:
Player.h: In member function 'void Player::ListUnits()':
Player.h:47: error: 'GetName' undeclared (first use this function)
Player.h:47: error: (Each undeclared identifier is reported only once for each function it appears in.)
I've tested enough to realize that the problem is the GameUnit::GetName() function is a public function within the GameUnit object. Why can't a Player call this function? Making both friend classes of each other doesn't work.
View 3 Replies
View Related
Aug 9, 2014
Why do you use Private fields in a class if an outside class can change the private fields using get and set properties?
View 2 Replies
View Related
Apr 4, 2014
I am trying to access the private member from main . The code is as follow. I wanted to call below function from main.
uint16_t Modbus::calcCRC(uint8_t u8length)
#define MAX_BUFFER 64
typedef struct {
[Code]....
View 1 Replies
View Related
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
Apr 24, 2014
Here in below code, the private data member is accessed directly using dot operator IN COPY CONSTRUCTOR and the program runs without error.
#include <cstdlib>
#include <iostream>
using namespace std;
class array {
int *p;
int size;
public:
array(int sz)
[code]....
View 1 Replies
View Related
Mar 19, 2014
#include <iostream>
#include <string>
using namespace std;
class book {
private:
string bookname;
double bookprice;
public:
book(string k="calculus",double b=25.5)
{ bookname=k;
bookprice=b;
}
View 2 Replies
View Related
Oct 30, 2013
I have a class (Quadtree) and three inner class inside (Node, Inner and Leaf). Inner and Leaf inherit from Node.
I have a function in the private scope of Quadtree.
All these are located in fun.h .
Then, in fun.cpp, I am implementing the function (which is named foo <- what a prototype name!), which takes as argument a pointer to an Inner object. Inner seems unable to be resolved however!
fun.h Code: class Quadtree {
private:
class Node{
public:
Node() { std::cout << "Node
"; }
};
class Inner : public Node {
public:
Inner() { std::cout << "Inner
[code]....
View 2 Replies
View Related
Sep 30, 2014
I want to make a destructor counter...So it needs to be outside that specific instance of the class. And i want to keep it hidden. I don't want to be able to access it from outside the class...I Don't want main.cpp to have access to this variable
Is there a way to do this?
static int destructorCount;
View 8 Replies
View Related
Feb 16, 2013
I get a problem with the vector as a private class member: When I did't initialize the vector in constructor(which means the size of the vector would be 0), I used a class function to add two elements to the vector and it worked (because I added a "printf" to output the size of the vector and the elements within that function). However, when I used another class function to visit that vector, no element was in and the size became 0.
Then I tried to add two elements to the vector during the construction, and it turned out that these two elements could be stored in the vector while other elements added through class functions could not.
I guess there may be some problems on the scope of the function. But I feel the class member should not be effected by the scope of the class function.
View 7 Replies
View Related
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
Apr 14, 2013
The reason that class members are private by default is because, in general, an object of a class should be a self-contained entity such that the data that make the object what it is should be encapsulated and only changed under controlled circumstances. Public data members should be very much the exception. As you’ll see a little later in the chapter, though, it’s also possible to place other restrictions on the accessibility of members of a class.
View 17 Replies
View Related
Mar 10, 2013
I'm trying to make a 3d game. I just started with opengl in c++ but I ran into a problem. What I have now.
Creating the array:
std::vector<Bullet*> bulls;
In the display function:
for (unsigned int i=0; i<bulls.size(); i++) {
[Code] ....
But when I try to compile this it gives an error:
cannot convert ‘Bullet::Get_prevx’ from type ‘GLfloat (Bullet::)() {aka float (Bullet::)()}’ to type ‘GLfloat {aka float}’
View 1 Replies
View Related
Jul 7, 2012
I've been writing my own implementation of quicksort in a .h file. I've gotten to the point where I think I'm done with the algorithm, so now I'm trying to make it so that if someone includes "quicksort.h", they will only have access to certain functions in the file. Kind of like public and private functions in a class, but without the "class" part of it. How do you do that?
View 7 Replies
View Related
Feb 9, 2013
So I'm trying to do a homework assignment, where I read a uml about a bank program, and just create it. Here is the UML. So while working on Bank, the top one, i've come up with this so far.
Code:
#include <iostream>
#include <string>
using namespace std;
class bank
{ string name;
int routingNum;
[Code] ....
I'm assuming that's what the uml wants. However I can't seem to access that private class. Idk why. I declared it as an object in the main.
View 4 Replies
View Related
Feb 10, 2015
I have a header file that declares some fields as private, I then have a class that I need to compare two of the objects' information for equality but neither of them are the calling objects. I cannot alter the header file. How would I go about comparing private data fields? I will enter a brief bit of code for clarity.
Code: // Header File
// stuff.h
class stuff
{
private:
int* arr[20];
int size;
};
bool equal (const stuff& a, const stuff& b);
[code].....
View 11 Replies
View Related
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
Jan 16, 2013
it seems that I cannot define a method of an inner nested class if it is a private class. for example:
class outter {
class nested {
void foo ( void ) {} // okay - but is this inline?
} void inner::foo( void ) {} // not okay - cannot define inside another class
} void outter::inner::foo( void ) {} // not okay - 'nested' class is private!
what I want to know is, is there another way to define an inner class's method? and if not, is it eternally doomed to be inline because it has to be declared inside it's own class declaration?
View 5 Replies
View Related
Jan 10, 2013
Let's say I have the following class:
class MyClass {
private:
int m_myInt;
public:
int myInt() {return this->m_myInt;};
int myFunc();
};
Which of the following is to prefer;
int MyClass::myFunc() {
return 2*this->m_myInt;
}
or
int MyClass::myFunc() {
return 2*this->myInt();
}
The second one seems better? Are both OK? What's the best practice?
View 13 Replies
View Related
Sep 19, 2014
I've been working on a program that uses a reference counting class that I've written which works fine for objects that inherit from it however I now need to have the following setup:
class SBComponent : private Refcounted { /*stuff*/}
class ProxiedComponent : public SBComponent, private Refcounted {/*stuff*/}
The compiler gives the following warnings
warning: direct base ‘Refcounted’ inaccessible in ‘ProxiedComponent’ due to ambiguity
And then several repeats of the error:
error: request for member ‘bk’ is ambiguous
Back *b = bk<ProxiedComponent>();
bk is a templated function that's defined in Refcounted and returns a pointer of type <template arg>::Back (ProxiedComponent::Back in this case).
I don't understand why the call to bk is ambiguous as although there's two instances of Refcounted (there needs to be with the way I've designed it so I can't make it virtual) it's inheritance is private in both cases so there should only be one instance of it visible in ProxiedComponent.
View 7 Replies
View Related
Aug 9, 2013
The question says that:
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
[Code].....
But then the 2nd part of the question itself says that "Acno entered by the user". So we need to identify/search for an account with that Account Number. How can we do this without comparing Acno with S.Acno? I know that S.Acno is not accesible as it is a private member but then how to compare Acno without even using a member function(public) that can return the value of Acno?
View 11 Replies
View Related
Jan 15, 2015
I just happened to find that a nested private template class can be accessed directly outside the enclosing class using a using alias:
namespace ns {
class __wrapper
{
private:
[Code].....
I was hoping to see a "__wrapper::__tklass is private" error message in the first using statement as well as during the instantiation of ns::tklass, but no error is issued. I tried this on gcc-4.9.2, clang-3.5.0 and visual_studio 2013 express.
why exactly doesn't the compiler flag tklass as an error? Is it allowed by the standard? If so, wouldn't that be a serious access violation?
View 2 Replies
View Related
Mar 30, 2013
how to access the private and protected member functions of the class.....
View 5 Replies
View Related
Mar 6, 2013
Below is a working program. Please note that the vector <int*> pointer is declared as a public member of the class A.
The problem arises when I try to make it a private (instead of public). First, let's look at the File 1 below. It compiles and works fine.
File 1: main.cpp (working fine)
#include <vector>
#include <iostream>
using namespace std;
[Code].....
View 19 Replies
View Related