I'm experimenting with a custom memory-pool for my application, and I initially planned to override the global new and delete operators to allocate memory in this pool. But since I'm using QT, this will apply to all the QT-related stuff as well. Should I instead just override the new and delete operators per class?
I have an assignment which includes overriding the prefix and postfix operators, and my teacher has provided what the output from the program should be. I've written the code and it's nearly perfect, except for one tiny error I can't seem to get right.
This is (most of) the code from the header--I left out a few of the parts that aren't relevant to my question:
Code: using namespace std; #include<string> #include<iostream> class NumDays { private: int hours;
[Code] ....
The two problem lines are supposed to be outputting 12 and 1.5, respectively, but are instead showing 13 and 1.625. I know that hours is being changed to 12 at the end of the overriden prefix operation in the line above them, so I don't understand why it returns to 13 again. What I need to change?
I'm looking at writing my own plug-in app, but I know that deleting class instances that were created in a plug-in module can result in the dreaded "undefined behaviour" because of the different memory spaces. Many examples of plug-ins use create_class and destroy_class functions to resolve this problem, but I wondered about overriding / overloading the class's new and delete operators. This would be used for all third-party library class derivations (e.g. derived GUI classes) and all home-grown classes.
The operators would only be declared in the class declaration:
class PluginBase { public: void *operator new(std::size_t n); void operator delete(void *p); // Other plugin bits
While the actual implementation would be defined in the plug-in's implementation file:
This would need to be implemented in every transferable plug-in class (possibly by a crafty IMPLEMENT_PLUGIN(classname) macro or some other mechanism), but before I commit this to my code I was hoping for feedback. Does this sound like a good idea? The GUI classes in particular are handled by a third-party library, so it's some memory-space safe way of deleting them by the GUI library (in the app) that I'm looking for.
I've got the following code with output. I can't figure out myself why it's what printed out there. I believe, it has something to deal with overloading/overriding/virtual functions implementations in C++:
class Base{ public: virtual void f(int); virtual void f(double); }
[Code].....
Thus here're my conclusions: 1) in line d.f(1.0); for some reason compiler preferred casting double->int of the argument and then call to 'Derived::f(int)'.
2)in line pb->f(1.0); for some reason compiler preferred call to 'Base::f(double);'. 'Base' is static type of pb, but the dynamic type is 'Derived'.
I believe the answer has to deal with the fact whether virtual table contains in addition to functions' names also the types of arguments they accept. AFAIK, vTable doesn't include such info.
class A //parent { public: virtual void DoSomething() = 0; };
class B : public A //child { public: void DoSomething(string s) override; }
Where the child member function overrides and changes the parents member function.
I need to pass an array of key states to the Controller class' Update() function but don't want to send it to every class derived from Object (like Controller).
Is this possible or do I have to overload the original Update() member function (but I would need to define the method in Object then (i.e remove the pure virtual function (=0)))
Below is simplified code consists of two classes, namely Parent and Child.
Child is inherited from Parent.
All member functions of class Parent are declared virtual, and they have been overridden in the class Child.
Code 1:
#include <cstdlib> #include <iostream> using namespace std; #define QUANTITY 5 class Parent {
[Code] ....
The output of the code:
Child::showID() -- ID is 1804289383 Child::showID() -- ID is 846930886 Child::showID() -- ID is 1681692777 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1957747793
Parent::operator=() invoked.
Child::showID() -- ID is 1804289383 Child::showID() -- ID is 846930886 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1957747793
Question:
Why is Parent::operator= invoked instead of Child::operator= ..?
Isn't it already declared virtual and hence would be overridden..?
I need to invoke Child::operator= instead. How to achieve this?
I create two memory manager called CMemManager1 and CMemMangaer2, using different algorithms to allocate buffer. Now I want to control which memory manager to be used, when calling new.
I try to add a parameter bUseWhichMemManager to the constructor, but in overrided new function, there are no way to access the parameter. Is there a way to pass more parameters to new operator, such as:
I have subclassed CFileDialog. I need to select both file and folder on certain case only. Suppose I have a folder selected and it is containing desired file type. Then in such situation, On clicking open button will not open the selected folder. But just close the CFileDialog with IDOK.
For doing this I need to provide my own implementation for Open button handler. I am not getting how I can do this.
I have to manage a Clinic. I need to delete a booking (for example, if John said he's coming on March 22nd at 15:30 but then he say he's not going to come, I have to delete that booking so another person can use it).
idSearched: the id of the person that is not going to come. I have a lot of specialties and each one has a list. So I ask for the speciality to delete the node (the node contains John's booking). If I don't find it, I return (-1). searchSpecByID return a pointer to the list where the speciality is. So head will point to the first node of the list. In nodeToDelete I have the node I want to delete.
The program detects OK when is the first in the list and when not, but it doesn't delete the node.
There was an "impovement" since Windows 7 in algorithm for selecting the initial directory, which is described here OPENFILENAME structure. Briefly:
Windows 7:
If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory. Otherwise, if lpstrFile contains a path, that path is the initial directory.
Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory. Otherwise, the initial directory is the personal files directory of the current user. Otherwise, the initial directory is the Desktop folder.
The problem that this behavior is not what users of my program expect. Another constraint is that I need to use old CFileDialog dialog, not Common File Dialogs. I've tried to use advises described on StackOverflow and on MSDN. This solution by EllisMiller works perfectly:
Specifying a full path (including filename) in lpstrFile. The filename of course shows up in the filename box which is annoying. I ended up using a filename of "." and adding a bit of code to clear the filename combobox once the dialog is open.
BUT I can't figure how to clear the filename combobox. I've tried to add hook procedure, enumerate windows and clear text, but this didn't work for me. So, my question is: how can I clear text in the filename combobox of CFileDialog?
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
I have two class GameOfLife and Cell and i want to overload square braket for class GameOfLife."if g is a GameOfLife object, g[10][5] will return the Cell at row 10 and column 5. If there is no such Cells, then it will return a new Cell with position (-1000,- 1000)."
but if g[10][1000] and 1000>cols,then it returns different Cell exp (3,2) How i do control the col ( [row][col] )?
#include<stdio.h> main() { int i=4,j=7; j=j||(printf("you can")&&(i=5)); printf("%d %d",i,j); }
output: 4 1
Although I am specifying the braces for the && operator so that it gets executed first..Then also the value of i remains 4 only..Why does not it gets changed to 5??Also the printf does not execute??
Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char).
So does that mean switch statements can only test if variable == value and nothing more, like > < >= <= != etc... ? I tried to make a program to test this and it seems like switch statements are limited to == but I'm not sure, maybe I'm doing something wrong.
This is the program I tried to make to test this:
Code: #include <stdio.h> int main () { int n;
[Code]....
So is it true that switch statements only work with the built in == operator? if that was the case then I would feel rather meh about switch statements.
I have a small piece of code that used the set::insert function on a set of myClass. For that, I need to overload the < and > operators, which I have, but I still get a huge error saying it can't compare.
set<MyClass> mySet; MyClass myClass
All the class information gets filled in. Then, I try to insert... mySet.insert(myClass);
bool operator<(MyClass &lhs, MyClass &rhs) { return lhs.name < rhs.name; //name is a string }
The error says ...stl_function.h:230:22: error: no match for 'operator<' in '__x < __y' MyFile.h:80:6: note: candidate is bool operator<(MyClass&, MyClass&)
void main() { int a=1; cout<<a++<<" "<<++a<<" "<<a++<<endl; }
If I execute the above program i should get 1 3 3. But I'm getting different values when I executed this program. The values that I get after execution are 3 3 1.
I made a program that allows the user to enter information of credit cards on an array of size 5, however I need to allow the user to compare the five credit cards with each other and I am having problems with this particular part. I made my bool operator functions for the operator< and the operator> but how to make the user be able to select which cards he wants to compare and how to compare them. My code is the following:
#include <iostream> #include <fstream> #include <string> using namespace std; const int SIZE = 5; enum OPCIONES {CARGAR=1, ABONAR, NADA};
What I'm trying to do is create a class for constructing an 'op tree' for parsing infix notation.
I started with a base class that uses a map of lambdas to actually calculate the operations (since they are mostly 1 line functions) of passed in integer or float values.
This base class just uses a templated T type as the lvalue and rvalue. I realized though that if I overload the math operators, +, -, etc.. I could also use the class itself as a type for the lvalue and rvalue. This lead me to think I could easily create the op tree by using Operation class members themselves as operands, which I think makes sense but I'm having some trouble expressing the code.
Example, if you look at the main() function I create normal operations easily with integer values. I then try to create a "tree" operation that includes 2 sub-operations as it's rvalue and lvalue, that is where I'm having some conceptual problems as far as implementing the code to do that.
what order a CPU would process the following arithmetic problem: 5 - (-9) = 14? Would the CPU recognize that the 'minus a minus' combination simply represents 5 + 9, and proceed with that addition, or would the CPU have to first calculate the 2's complement of -9, and then proceed to take the 2's complement of that first result in order to complete the calculation of the addition of the 'double negative'?
1.The operands from << and >> may be any of integer type (including char) The integer promotions are performed on both operands the result has the type of the left operand after promotion.
It means that if we have z = x >> y then sizeof(z) == sizeof(x) ?
2. The ~ operator is unary the integer promotions are performed on its operand.
So if I have short int y; and int x=1; y = ~x what is the meaning here?