C++ :: Virtual Methods With MI
Jan 6, 2014
I have questions about multiple inheritance and virtual methods. I have a class called solid. All objects of this class have hitboxes and can collide with others. I have the following methods:
void testCollision(something begin, something end);
/* This method takes a container's begin and end iterators to test if the object collides with any other object of the list of all the solids currently in the game area. Each time there is a collision, it calls collide(other) and other.collide(*this) */
virtual bool collide(solid& other);
/* This method always returns false and does nothing */
This class will be inherited by another class which will have overloads for a few specific collisions. For example:
class player : public solid{
public:
bool collide(projectile& other);
bool collide(enemy& other);
bool collide(wall& other);
};
My question is quite simple actually. If I have a loop which calls testCollision() with all elements in the list of all solids (a list of pointers to solids to be exact) and there is a collision between the player and a projectile, will testCollision call player::colide(projectile& other) or will it call solid::collide(solid& other). And in any case, did I understand how to use the virtual keyword? If I'm right, it should call the player::colide method if it's there for the specific type, else it will call the solid::colide which only returns 0, ignoring collision.
View 4 Replies
ADVERTISEMENT
Jul 11, 2012
I develop add-ons for MS Flight Simulator and I use the poorly documented SDK for this. The SDK provides a .h file in which an interface class is defined (with pure virtual methods only), something like this:
Code:
class IPanelCCallback {
public:
virtual IPanelCCallback* QueryInterface (PCSTRINGZ pszInterface) = 0;
virtual bool ConvertStringToProperty (PCSTRINGZ keyword, SINT32* pID) = 0;
virtual bool ConvertPropertyToString (SINT32 id, PPCSTRINGZ pKeyword) = 0;
};
In my code, I use this interface like this:
Code:
IPanelCCallback* pCallBack = panel_get_registered_c_callback("fs9gps");
...
SINT32 id;
pCallBack->ConvertStringToProperty(propertyName, &id);
Everything works fine, but I don't understand why... I thought the linker would stop with an "undefined symbol" error because the IPanelCCallback methods, such as ConvertStringToProperty, are declared as pure virtual but defined nowhere, and I don't use any library for linking. With such an interface class, I thought I would have to defined a subclass of IPanelCCallback and define the ConvertStringToProperty method.
View 6 Replies
View Related
Dec 27, 2012
class Parent{
public:
virtual int width();
virtual int height();
int area(){return width()*height();};
[Code] ....
View 10 Replies
View Related
Feb 15, 2012
I have h-file with definition of 3 classes. And cpp-file with realization methods of this classes. And when I compile the project errors - LNK2019 is apperead... How make that methods become visible?
View 1 Replies
View Related
May 7, 2013
I'm writing a sorted vector implementation and trying to do it as simply as possible.
Right now, I'm declaring the sorted vector with a protected subclass of vector, then using the "using" keyword to explicitly inherit all methods that aren't related to adding new elements to the vector (so I can control the order).
Eg:
Code: template<typename T, class Cmp = std::less<T>>
class sorted_vector : protected std::vector<T>{
public:
typedef typename std::vector<T>::iterator;
using std::vector<T>::operator=;
using std::vector<T>::assign;
using std::vector<T>::get_allocator;
using std::vector<T>::at;
using std::vector<T>::operator[];
//...
};
Obviously, this is annoyingly redundant. So what I'd like to do is something using the new "delete" keyword from C++11. Is there any quick, expressive way of deleting specific methods?
Also, it's pretty annoying to have to typedef base_class::type type to inherit a type from a base class. Is there a shorter way to do that?
View 14 Replies
View Related
Nov 21, 2013
How to i access setter from within a setter?
I know that getter methods can be access like
Code: getAccounts().getAccountNumber();
// for example but for setters how do i do it? this doesnt seem to work though
Code: setAccounts().setAccountNumber(accountNumber);
View 7 Replies
View Related
Oct 20, 2012
Is it common practice to package methods with other elements(string-s, int-s, float-s, array-s...) of struct?
Than struct acts as a class or is acting similar to class and that have no sense to me? I am new to OOP .
View 4 Replies
View Related
Feb 27, 2014
how to call the methods of the class.I have an object call v which is an array and I don't how to call the methods of the class. The error is here:
v.readDates(v[a]);
#include "Date.h"
#include <iostream>
using namespace std;
int main(){
int a;
cout << " HOW MANY DATES DO YOU HAVE? " << endl;
[code]....
View 3 Replies
View Related
Oct 9, 2013
I'm using lua 5.2.2 with luabind 0.9.
I'd like to be able to add additional class-methods through lua for any classes that I've bound in c++, but I'm unsure how to do it.
The problem is that luabind uses a function as the __index-metamethod for any bound classes instead of a table, so I don't see a way to access the class-methods at all.
e.g., I'm binding my classes like this:
luabind::module(lua) [
luabind::class_<testClass>("TestClass")
.def(luabind::constructor<>())
.def("TestFunc",&TestFunc)
];
What I essentially want to do is to add a lua-function to the list of methods for this class, and be able to overwrite existing ones:
local t = tableOfClassMethods
local r = t.TestFunc -- Reference to the c++-function we've bound
t.SomeFunction = function(o) end -- New function for all objects of this class
t.TestFunc = function(o) end -- Should overwrite the c++-function of the same name
View 2 Replies
View Related
May 8, 2013
i have been trying to call out the tea algorithm from wikipedia. However i keep getting a segmentation fault. Am i calling it out the wrong way? Below are the snippets
methods taken from wikipedia
void encrypt (uint32_t* v, uint32_t* k) {
uint32_t v0=v[0], v1=v[1], sum=0, i; /* set up */
uint32_t delta=0x9e3779b9; /* a key schedule constant */
uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
for (i=0; i < 32; i++) { /* basic cycle start */
sum += delta;
[code]....
i tried to encrypt a test text and casting it to the uinstd32_t type. However it always gives me a segmentation fault.
View 2 Replies
View Related
Feb 2, 2014
What is a method and what is an object?
View 8 Replies
View Related
Jul 3, 2013
Creating the methods for class List
main.cpp Code: #include "List.h"
int main( )
{
List la; // create list la
la.push_front( "mom" );
la.push_back( "please" );
la.push_back( "send" );
la.push_back( "money" );
la.push_front( "hi" );
cout << "
la contains:
" << la << '
[code]...
View 12 Replies
View Related
Jan 29, 2012
I know that memory addresses in the stack can contain either values or references to other memory addresses, but do these memory addresses also contain methods or are the methods themselves located in the heap?
The confusion comes from the fact that in C# a delegate variable can be assigned either a method's identifier, an inline function, a lambda expression, or a new instance of the delegate type with the method's identifier passed as an argument to the constructor. My guess is that assigning the method's identifier directly to the delegate variable is just a simplified way of calling the delegate type's constructor with the method's identifier as an argument to the parameter, something that the compiler handles for you.
But even in this last case, the delegate variable is said to point toward the method itself. In that case, does it mean that methods are stored in the heap, just as reference type values are?
View 2 Replies
View Related
Oct 30, 2013
I need to override a few methods in FILE class so i defined few methods as
EnCrpt * fp;
fp * fopen(const char * filename, const char * mode);
int fwrite(const void * p,int length,int readLenth,FILE * fpp = NULL);
int fread(void * p,int length,int readLenth,FILE * fpp = NULL);
int fseek(FILE * fpp = NULL,long offset, int whence);
long ftell(FILE * fpp = NULL);
int feof(FILE * fpp = NULL);
int fflush(FILE * fpp = NULL);
int fclose(FILE * fpp = NULL);
I will call fread method in my encrypted file class .. similar to other methods.. is this correct ? can NULL file pointer create issue ?
Because i have so many place where FILE class called i don't want to change everywhere to call encrypted file class so i am override these methods to encrypted file class instead of standrd FILE class
View 9 Replies
View Related
May 17, 2013
Suppose I have a class "A", which has a method "void AMP_call()" that calls paralel_for_each in which another method, "float amp_function(float) restrict(amp)". When I call that method, can it then use members of "A"?
class A {
void AMP_call();
float amp_function(float) restrict(amp); // do something on a device
float allowed_variable;
std::vector<bool> not_allowed;
[Code] ....
Another way to frame my question, perhaps to make it easier to understand what I am after, would be that I want to know what happens if an amp-restricted method is called where the body of the class itself (which is not amp-compatible and afaik doesn't have to be since it's not passed to the device) may contain members that are not amp-compatible.
All of the msdn blogs I could find deal with which functions and methods can be called from within a parallel_for_each loop, but not with which variables are available to the lambda function itself.
View 9 Replies
View Related
Sep 30, 2014
I need to write a windows form application that allows the user to enter a credit card number and credit card type and then determines whether it is a valid number using the following rules:
1.) the first number is:
a. 4 for visa
b. 5 for master card
c. 37 for american express
d. 6 for discover
2.) passes the mod 10/ luhn check, which is calculated as:
a. from right to left, multiply every other digit by 2. when doubling a digit results in a two digit number, add the numbers to get
a single digit. ( like 6 * 2 = 12 therefore 1 + 2 = 3).
b. add all the single digits from 2a
c. add all the odd places from right to left in the card number
d. sum the results fro steps 2b and 2c
e. if the resulting number in 2c is divisible by 10, the card number is valid otherwise it is invalid.
The chapter is introducing methods so that is the main thing i will be using for each step.
View 1 Replies
View Related
Jun 17, 2014
I am given 2 methods and they want me to create a stand alone class for sql that will change the sql string.
my question is how can i take these 2 methods and make one class out of them that will used on various other forms.
#1
public string AuthenticateWithDB(CUiPontisDatabaseSpecification pdb, string sUserId, string sPassword,
bool bCreatePersistentCookie)
#2
public static void ChangeConnection(Util.ODBC.ODBCDSN odbcInfo, CPonDatabaseVendorType dbType, string uid, string password)
they want it so they can use it like
CUiHttpSessionManager.SimpleDataConnectionString = SomeNewClass.CreateSimpleDataConnectionString()
where some new class is my new class
View 5 Replies
View Related
Aug 17, 2012
I have a template class which defines a few heavy methods. For now, they are defined in the same .h file as the class definition, but i`d like to have them in a separate .cpp file.
A situation i find you describe in the FAQs arises: [URL] ....
Problem: the export keyword has been deprecated in c++0x, if i recall correctly, and has never been implemented in any of the compilers i am using (msvc, gcc).
#Including the the .cpp file after the class definition (as described in the second post of the FAQ) works.
another question: i have methods that dont use any template code. Can i somehow declare them as such? (more of an esthecial question, which would make it easier to distinguish between template and non.template code).
View 6 Replies
View Related
May 29, 2014
I've been experimenting with char arrays and getting user input through different methods.
int main() {
char userInput[21];
/*I understand that over here, a maximum of 20 letters can be input, and only letters before a space will be stored in userInput*/
std::cin >> userInput;
std::cout << userInput << std::endl;
[Code] ....
As I was testing, whenever I would input a single word for userInput (for example "hi"), the program would work as expected: it would output "hi" and I'd be able to input a sentence of sorts for userInput2 (for example "hello world") and have it outputted.
But if I were to input more than one word for user Input (for example "hi how are you"), the program would output "hi" as expected, but it wouldn't let me input anything for userInput2 and would just output the rest of the first input; in this case, "how are you" would be outputted and the program would end. I am not aware of the logic error at play.
View 7 Replies
View Related
Jan 16, 2013
i want to write a program that can access to variables and methods of another program. I ques for access to variables need to read memory, but about methods?how i can use methods and call them?
View 1 Replies
View Related
Apr 11, 2014
There is any method to compile a method of a class separately from the other methods and the main.
View 1 Replies
View Related
Apr 24, 2014
I have several functions doing similar things, inside their implementations, the parameters are the same, but they call different methods.
I want to create one function to make the structure easier, and reduce the duplication. I heard template might be one solution. But I am not sure how to use it in this case.
void GetA(...XXX) {
for()
{
[Code].....
View 1 Replies
View Related
Mar 21, 2015
The only difficulty im having is creating a class and methods & being able to access them from my win form app. Can i get a few tips on the do's and donts of creating classes / methods and accessing them from form app.
This is what i have put together so far.
public partial class Form1 : Form {
private Image[] dicePics;
private int[] diceNum;
private Random randomize;
public Form1() {
InitializeComponent();
[code]....
View 3 Replies
View Related
Oct 2, 2013
Imagine if there is an abstract class with a method (say output or print) which would be inherited by a few other classes. Later objects are created using the inherited classes, and the user wishes to call the above method twice, for eg (i) output/print to screen and (ii) output/print to a file. What is the best way to achieve that.
View 6 Replies
View Related
Jun 24, 2013
I am having a problem on how will I create a large matrix to be solved using numerical methods. Say I have a 500 x 500 matrix and want to find these 500 equations. How will I do it? To be exact, here is a sample problem
| -2 1 . . . . . . . || x1 | |h^2|
| 1 -2 1 . . . . . . || x2 | |h^2|
| 0 1 -2 1 . . . . . || . | | . |
| 0 0 1 -2 1 . . . . || . | = | . |
| . . . . . . . . . 1 || . | | . |
| . . . . . . . . 1 -2||x500| |h^2|
Where h = 1/(n + 1)
Solutions on finding would be gauss elimination, Successive overrelaxation, Jacobi Iteration, biconjugate gradient method and Thomas algorithm. I want to compare the accuracy, with tolerance of 10^-6
View 2 Replies
View Related
Jun 27, 2013
The problem is the following, I am making a wrapper object of sockets in c++, since the ones in c are somewhat ugly, and not quite OOP. I can't use boost sockets since the project I am currently working on, can only use the libraries found in ubuntu 12.04 repositories, and the ubuntu 12.04 repositories are on boost-1.46. My solution was the following, three classes, AbstractSocket, ServerSocket, and Socket. AbstractSocket is the superclass of both ServerSocket and Socket. The following is the class definition of each of these classes:
class AbstractSocket {
public:
AbstractSocket();
AbstractSocket(const int file_descriptor, const struct addrinfo* local_address,
[Code].....
My problem and question is the following, on ServerSocket::accept method I have to do a "client_socket = new Socket(abstractSocket);", and in Socket::recv_all method I do something like "buffer = new unsigned char[bytes_to_receive];". These two situations are somewhat problematic to me, because I don't wish to make the caller of the methods responsible for deleting this memory.
Therefore, is there an elegant way to retrieve heap allocated space from a function/method? Or better yet, is there an elegant way of telling the caller of the method, that the memory will have to be deleted?
I thought of placing an "unsigned char* buffer" inside Socket class, and to delete the memory allocated in the destructor and on consecutive calls to recv_all, but i don't believe this is the solution, and it doesn't solves me the problem for the "accept" method.
View 3 Replies
View Related