C++ :: Memento Pattern Failing Because Of Pointer Data Members
May 24, 2014
All the undos and redos were working fine until the pointer data members came into the picture. The problem is that the values pointed to changed, but the pointers themselves did not. So restoring gives the same pointers but they still point to the new values. I think the solution is that the classes that are the pointer data members themselves need their own Mementos (which would be a lot of work because there are many data member pointers in my program). Is that the only approach?
Here is a sample code to show what I'm talking about. You can compile and run the program to see the problem it has restoring values of pointer data members:
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <list>
#include <cstdlib>
#include <ctime>
class FoodHistory {
[Code] ....
View 2 Replies
ADVERTISEMENT
May 6, 2014
The code below runs correctly (feel free to compile and run it yourself to see what it does). But when I turn Object abstract by uncommenting the line
virtual void foo() = 0;
It obviously won't compile. I want to use a copy constructor rather than restoring one data member at a time because I'm assuming that Object will have many, many data members (and also want to avoid extra responsibility when new data members are added to Object). Of course, using a pointer to Object in Memento will defeat the purpose of taking a snapshot.
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
const std::string NAME = "Object";
[Code] .....
View 9 Replies
View Related
Feb 22, 2012
environment : qt creator 4.7
code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class singleTon {
[Code] ....
this is a singleton pattern first,it doesn't matter, why I could delete this pointer twice?because the gcc compiler?That mean in the surface, "delete pInstance1;" this movement just mark the memory pInstance1 has been deleted but not real?does any one encounter this phenomenon?
View 7 Replies
View Related
Sep 4, 2012
I am trying to use 'this' pointer but i am confused why 'this' pointer is not available for static member functions.
Code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
const int MAX = 20;
const int MAXPTR = 100;
class name {
private :
char fname[MAX], mname[MAX], lname[MAX];
[code].....
I am using GNU GCC Compiler via Code::Block
Error : 'this' is unavailable for static member functions
View 4 Replies
View Related
Apr 19, 2013
I have a class buffer, which holds a std::string as member, and a socket_receive function:
struct buffer {
string data;
buffer() {}
buffer(buffer& b) : data(b.data) {}
};
buffer socket_receive() {
buffer tmp;
tmp.data = "1234";
return tmp;
}
so when I write
buffer b = socket_receive();
there is a copy constructor, and the tmp variable is constructed and destructed, is there anyway to improve the performance?
I tried the new c++11 move(), but seems even worse, I must be doing wrong with move(), but I don't know how.
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
struct buffer {
string data;
[Code] .....
View 4 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
Mar 10, 2013
I'm trying to put file data into members of a class. Remember to type in the file name you want to open. Cool feature right? I just had Dbase.txt so I chose that.
Fixed stuff in the .txt. Now I need to figure out why it only does 1 set and then ends.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
class INFO {
[Code] .....
Dbase.txt:
Bob
Guy
Programmer
M
9999.99
40
------------------
Little
Guy
Little Brother
M
0.0
3
------------------
View 14 Replies
View Related
Mar 6, 2014
Overview of problem : I am using std::vector to hold objects of Subject. Now this vector contains lots of objects( with lots I mean 10-20 objects at max) . These objects have string member values like category and sub_category. Both category and sub_category can have string which can be same of other objects's sub_category & category.
Issue: Now I want my std::vector to have only those objects whose's sub_category are unique. If category is not unique that's not a problem .
Secondly if we found 2 objects having same sub_category then we have to delete one of them from the vector. we will delete it based on some rules example
Rules for deleting are if
i) instance of Subject ->category = " Land " OR if category = "Jungle" then delete other duplicate object ,
ii) if above condition doesn't match then delete either of them.
I am wondering , how would I compare the sub-items from the vector . For example. I have class say Subject
class Subject {
public :
// some constructors,
// functions to get ., set category and sub category
std::String get_sub_category()
std::string get_category();
private:
std::string category;
std::string sub_category;
}
I have vector which stores object of Subjects. Example : vector<Subject> copy_vector;
Now what I want is to delete the object from vector that has same sub_category I am not looking for source code buT i need a starting point,? Example:
copy_vector[0] = Animal object that has sub_category Tiger
copy_vector [1] = Animal object with Lion as sub category
copy_vector[2] = Forest object with sub_category Tiger
What I want is to based on some conditions(which I can do ) remove either Forest or Animal object containing Tiger. But for that how would I do comparison? I have written the function and have checked it.
std::vector< Subject >copy_vector;
// copy_vector contains all the objects of Subject with redundant sub_category
for( std::vector< Subject >::iterator ii = copy_vector.begin() ; ii != copy_vector.end() ; ++ii ) {
sub_category = ii->get_sub_category();
[code] ....
View 1 Replies
View Related
Apr 16, 2014
The program is supposed to read a formatted .txt file and store the data into their respective [Class] Members. It will then output the data collected onto screen.I overloaded ifstream and istream. When I call for file>>ptr[i] to read the data, something goes wrong. It prompts me to input data (which is in istream overload).The Data in .txt file is as follows;
#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
[code]....
View 6 Replies
View Related
Jun 26, 2012
I am to first increment data members of object that has not created dynamically (i have done with this part),now i have created object dynamically and how to increment its data which i have passed as argument as:
obj3 = new manage(35 , 36)
View 4 Replies
View Related
Nov 4, 2014
Is it generally better to initialize string data members as nullptr or as a zero-size array?
I can understand the former is superior from a memory-use perspective and also not requiring the extra allocation step. However, many string management functions will throw an exception - wcslen for instance - if you pass them a null pointer. Therefore I am finding any performance gained is somewhat wiped out by the extra if(pstString==nullptr) guards I have to use where it is possible a wchar_* may still be at null when the function is called.
View 4 Replies
View Related
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
Oct 7, 2013
I have to merge two sorted files. Algotrithm that i'm using is below, but it reads not all the numbers in the files, and stops, even the files contain the same number of elements. What is wrong, I can't understand.
ifstream f1("E:desc1.txt");
ifstream f2("E:desc2.txt");
ofstream f("E:desc.txt");
cout <<endl<< "Nuerele sortate: " <<endl;
int n1, n2;
f1 >> n1;
f2 >> n2;
[code]...
desc1.txt is:
99 97 95 93 91 89
and desc2.txt is:
100 98 96 94 92 90
and I'm getting this:
100 99 98 97 96 95 94 93 92
View 1 Replies
View Related
Feb 5, 2014
I get a runtime error when I add a destructor to my code.
Code:
#include <iostream>
#include <array>
using namespace std;
struct nodes { int* elements; };
class heap
[code]....
View 14 Replies
View Related
Sep 30, 2013
I have a code in which I am passing string "5368709120" to function strtoull() and it should had returned me number 5368709120 but instead it returns me 1073741824 which is incorrect.
What may be the possible cause of this and how to rectify it?
Code:
typedef unsigned long long
ULL_Type;
char *quotaStr = "5368709120";
ULL_Type quota;
quota = strtoull(quotaStr, NULL, 10);
View 3 Replies
View Related
Feb 20, 2015
I'm trying to move a dictionary into a group of bite-sized files based off of the length of the strings in each file (I'm ignoring strings of length 1 for obvious reasons). Since I don't know what the longest word is (and I'm not going to look for it), and I don't want redundant files, I decided to use a vector of output file streams that I would put all the words into, but I can't get the file to open.
I took a look at it and found the failbit is 1, but the badbit is 0, so apparently it's a logic error on opening the stream [URL] .... I looked online and decided to try using pointers, but that didn't work either, so now I'm asking what the problem might be, because I can't think of any reason why it isn't working.
void createDictionary() {
//declare variables
string dictionaryName;//name of dictionary file
ifstream dictionaryIn;//file of original dictionary
vector<shared_ptr<ofstream>> dictionaryOut;//files where dictionary will be put based off of word length
string word;//input from the dictionary file
vector<int> lengths;//vector containing available lengths and their locations
unsigned long long counter = 0;
[Code] ....
View 2 Replies
View Related
Aug 4, 2014
Here's the source code from "battle.h" (which should probably be renamed to "animal.h")
Do you think I made my errors here or in one of the many other headers or cpp?
#include "Identity.h"
#include "stdafx.h"
#ifndef battle_h
#define battle_h
int numbers1 = rand() % 5;
int numbers2 = rand() % 7;
int numbers3 = rand() % 9;
int numbers4 = rand() % 11;
[code].....
View 8 Replies
View Related
Sep 24, 2012
I have a C++ code that reads the database password from the registry and decrypts it using CryptUnprotectData. We have to deploy this application on a Windows 7 machine.
When I compile my code on Windows XP and run it on the test Windows 7 machine, it works absolutely fine. When I compile the same code on my laptop having Windows 7 and run it on the test Windows 7 machine, CryptUnprotectData fails with GetLastError() return '87'. If I run this application on my own laptop with Windows 7 on it, it again works fine probably because my laptop has a lot of things installed including Visual Studio and all the service packs etc.
I believe I have missed out installing some dependency on the test Windows 7 machine but I am unable to figure out what is that. What is it that's making the Windows XP compiled code running fine on the test machine and not the code that's compiled on Windows 7.
Here is the call to CryptUnprotectData in the code:
if (CryptUnprotectData(
&cipherText,
NULL,
NULL, // Optional entropy
NULL, // Reserved
NULL, // Optional PromptStruct
CRYPTPROTECT_UI_FORBIDDEN,
&plainText))
[code]....
View 10 Replies
View Related
Aug 17, 2013
I am supposed to implement the member functions of class Person.
class Person {
public:
Person();
Person(string pname, int page);
void get_name() const;
void get_age() const;
[Code] ....
The code I wrote is below. Where I am struggling is the program does not allow me to input age. Therefore, I cannot test if my temp for age works. It automatically defaults to 0 because it hasn't taken input. Here is my code:
// Program Title: Person function
// Program Description: The program prompts the user for first and last name and age.
// It then prints the output that was provided by the user.
#include<iostream>
#include<string>
using namespace std;
class Person {
[Code] .....
View 13 Replies
View Related
Jun 22, 2014
So, I've got this class in SDL Player that has, among other things, an SDL_Texture* to hold an image that represents the player on the screen. I'd assume it's good practice to do get() and set() functions for the class; but because textures are handled via pointers, when I write a get() function I end up returning a pointer to an internal resource; which isn't good practice I hear as it "breaks" encapsulation.
Find my code below:
#ifndef PLAYER_H
#define PLAYER_H
#include "SDL.h"
#include "SDL_image.h"
#include "CTexture.h"
class Player {
[Code] .....
View 1 Replies
View Related
Jul 31, 2013
I have 3 arrays named: abee1 , abee2, myarray.
I have loop that want to call abee1 , abee2 one each time and copy myarray into it . It means that I reach the name of abee1 to abee2 in the loop for doing this. I made the name of each one by using strings ( abee1 , abee2) in the loop. In the last I want to copy the myarray into the abee1/2 using pointer.
I do not know what to do after the line "string arrayname = "abee" + String;".
#include<iostream>
#include<string>
#include <sstream>
using namespace std ;
int abee1 [4] [4] ;
[Code] ...
View 1 Replies
View Related
Dec 4, 2013
I have two classes, productListType and buyerListType, that are each basically linked lists. To create the linked list for an object of productListType, I successfully wrote a non-class function createProductList to read the product data from a text file. The class definition for buyerListType has a data member productBoughtRecord that is of type productListType to aggregate the details of the products purchased by a particular buyer during transactions. I decided to make productBoughtRecord a pointer since the contents of this data member would wax and wane over the course of several transactions, depending on the amount and frequency of payments made by the buyer. I have provided a rough sketch of the class below:
class buyerListType
{
public:
setCustomerInfo( ....., productListType* p);
.
.
.
private:
productListType* productBoughtRecord;
.
.
};
I'm similarly trying to write a non-class function createBuyerList to load the record of customers from a text file. How do I determine what the value of the formal parameter p in member function setCustomerInfo is, in order to be able to set the data member productBoughtRecord? take into consideration that an object of class buyerListType would have multiple buyers with varying amounts of products purchased.
View 11 Replies
View Related
Nov 20, 2014
I have a piece of code that sorts data based on some metric. The some metric is something I now want to make flexible so I can easily switch between and compare metrics. To do this, I want to pass the function to use as a parameter to the method that does the sorting (and other stuff). However, I'm having problems figuring out the syntax. Also, I think my [temporary] organization of code is violating a lot of basic code design principles.
To make the function pointer passable, I defined the "typename" in the header where the function is located (it is part of a struct, "Data"):
// Below the struct definition of Data
typedef double (Data::*CostF)(unsigned l, double W) const;
The two example functions I want to use are defined in that struct ("Data"):
// Inside the struct definition
inline double someExampleCost(unsigned l, double W) const {
// Returns some basic calculation
}
The function that uses it is part of a different class (that holds a reference to the first class, in case that matters; I feel like I'm doing something odd here, because I'm binding a member function in the definition/passing, but never referencing the object). It looks like this:
// Inside another class ("Foo")
inline void DoSomeStuff(double& ECost, double& TCost, CostF cost) {
// Irrelevant stuff here
std::sort(vector.begin(), vector.end(), [&](unsigned a, unsigned b){
return (*cost)(a, W) < (*cost)(b, W);
});
// More irrelevant stuff here
}
The error shown is "operand of "*" must be a pointer". If I remove the '*':
[code]return cost(A, W) < cost(b, W);
the error becomes: "expression must have a (pointer-to-)function type."
The call to this function is, currently, just in the main function, as I'm just testing before I wrap it into real code. It looks like this:
// In main
Foo bar; // Make an object of the struct that has the "sorting" function
CostF costFunction = &Data::someExampleCost;
// Bind to the Cost function bar.DoSomeStuff(varA, varB, costFunction);
This bit shows no errors by itself. So, my questions:
a) Clearly I'm missing the insight into Function Pointers. I'm comfortable with regular pointer stuff, but I can't wrap my head around FPs, partly due to the awkward syntax.
b) I'm very uncomfortable with the fact that I'm binding a member function of a class, but never try to reference an actual object of that class. This is probably a big part of why it's not working, but I can't seem to bind a function belonging to a specific object. I thought of doing
// In the main again
Data d; // Construct the object, which contains big lookup tables
Foo F(d); // Construct the object, which only holds a reference to a Data object
CostF costFunction = &d.someExampleCost; // Bind to the Cost function of that object
but that doesn't work ("a pointer to a bound function may only be used to call the function").
View 4 Replies
View Related
Jul 4, 2014
I've been working on this code that should be looking like
1 3 5 3 1
3 5 7 5 3
5 7 9 7 5
3 5 7 5 3
1 3 5 3 1
but mine appears like this
1 3 5 3 1
3 5 7 5 3
5 7 9 7 5
3 3 5 7 9
1 3 1 3 5
This is the code I've got for now, I just don't know which logic I need to fix
int main(){
int r;
cout << "Rows?" << endl;
cin >> r;
for (int i = 1; i <= r; i+=2){
for (int j = i; j <= r+i; j+=2){
cout << j<< " ";
} for (int k = i+2; k >= i-1; k-=2){
[code]....
View 2 Replies
View Related
Oct 17, 2014
I have spent at least an hour trying to display this pattern and I am not getting anywhere.
Pattern C
1
21
321
4321
54321
654321
This is as far as I got
#include <iostream>
#include <iomanip>
[Code]....
View 4 Replies
View Related
Nov 3, 2014
I'm trying to output a pattern using loops. The pattern is a plus symbol made of 3 rows of 5 x's followed by 3 rows of 15 x's and finally 3 rows of 5 x's.
I can get the program to output all 9 rows with 5 x's but I don't know how to do the 3 rows of 15 in the middle. I have tried a while loop and also an if statement but nothing seems to work.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
[Code]....
View 8 Replies
View Related