Search Results

Search found 53294 results on 2132 pages for 'null pointers etc'.

Page 8/2132 | < Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >

  • Vector of pointers to base class, odd behaviour calling virtual functions

    - by Ink-Jet
    I have the following code #include <iostream> #include <vector> class Entity { public: virtual void func() = 0; }; class Monster : public Entity { public: void func(); }; void Monster::func() { std::cout << "I AM A MONSTER" << std::endl; } class Buddha : public Entity { public: void func(); }; void Buddha::func() { std::cout << "OHMM" << std::endl; } int main() { const int num = 5; // How many of each to make std::vector<Entity*> t; for(int i = 0; i < num; i++) { Monster m; Entity * e; e = &m; t.push_back(e); } for(int i = 0; i < num; i++) { Buddha b; Entity * e; e = &b; t.push_back(e); } for(int i = 0; i < t.size(); i++) { t[i]->func(); } return 0; } However, when I run it, instead of each class printing out its own message, they all print the "Buddha" message. I want each object to print its own message: Monsters print the monster message, Buddhas print the Buddha message. What have I done wrong?

    Read the article

  • array of pointers

    - by tushar
    char *a[]={"diamonds","clubs","spades","hearts"}; char **p[]={a+3,a+2,a+1,a}; char ***ptr=p; cout<<*ptr[2][2]; why does it display h and please explain how is the 2d array of ptr implementing and its elements

    Read the article

  • Member variable pointers to COM objects

    - by drelihan
    Hi Folks, Is there any problem with keeping member variable pointer refernces to COM objects and reussing the reference through out the class in C++. Is anybody aware of a reason why you would want to call .CreateInstance every time you wanted a to use the COM object i.e. you were getting a fresh instance each time. I cannot see any reason who you would want to do this, Thanks, (No is an acceptable answer!!!)

    Read the article

  • Arrays of pointers to arrays?

    - by a2h
    I'm using a library which for one certain feature involves variables like so: extern const u8 foo[]; extern const u8 bar[]; I am not allowed to rename these variables in any way. However, I like to be able to access these variables through an array (or other similar method) so that I do not need to continually hardcode new instances of these variables into my main code. My first attempt at creating an array is as follows: const u8* pl[] = { &foo, &bar }; This gave me the error cannot convert 'const u8 (*)[]' to 'const u8*' in initialization, and with help elsewhere along with some Googling, I changed my array to this: u8 (*pl)[] = { &foo, &bar }; Upon compiling I now get the error scalar object 'pl' requires one element in initializer. Does anyone have any ideas on what I'm doing wrong? Thanks.

    Read the article

  • Accessing structure through pointers [c]

    - by Blackbinary
    I've got a structure which holds names and ages. I've made a linked-list of these structures, using this as a pointer: aNode *rootA; in my main. Now i send **rootA to a function like so addElement(5,"Drew",**rootA); Because i need to pass rootA by reference so that I can edit it in other functions (in my actual program i have two roots, so return will not work) The problem is, in my program, i can't say access the structure members. *rootA->age = 4; for example doesnt work. Hopefully you guys can help me out. Thanks!

    Read the article

  • Help with pointers in Cocoa

    - by G.P. Burdell
    I'm trying to make a simple calculator application in cocoa. The program hangs when I click on one of my buttons. I think I've traced the problem to the part of my controller that adds a digit to the end of the number currently on the display: - (void)updateNumber:(int)buttonClicked{ *self.activeNumberPointer = *self.activeNumberPointer * 10 + buttonClicked; [outputField setFloatValue:*self.activeNumberPointer]; } I used a pointer to the "activeNumber" in order to allow my program to tell which of the two operands I'm editing. Any help appreciated, thanks.

    Read the article

  • In C: sending func pointers, calling the func with it, playing with EIP, jum_buf and longjmp

    - by Yonatan
    Hello Internet ! I need to make sure i understand some basic stuff first: 1. how do i pass function A as a parameter to function B? 2. how do i call function A from inside B ? now for the big whammy: I'm trying to do something along the lines of this: jmp_buf buf; buf.__jmpbuf[JB_PC] = functionA; longjmp(buf,10); meaning that i want to use longjmp in order to go to a function. how should i do it ? thank you very much internet people ! Yonatan

    Read the article

  • C pointers and addresses

    - by yCalleecharan
    Hi, I always thought that *&p = p = &*p in C. I tried this code: #include <stdio.h> #include <stdlib.h> char a[] = "programming"; char *ap = &a[4]; int main(void) { printf("%x %x %x\n", ap, &*(ap), *&(ap)); /* line 13 */ printf("%x %x %x\n\n", ap+1, &*(ap+1), *&(ap+1)); /* line 14 */ } The first printf line (line 13) gives me the addresses: 40b0a8 40b0a8 40b0a8 which are the same as expected. But when I added the second printf line, Borland complains: "first.c": E2027 Must take address of a memory location in function main at line 14 I was expecting to get: 40b0a9 40b0a9 40b0a9. It seems that the expression *&(ap+1) on line 14 is the culprit here. I thought all three pointer expressions on line 14 are equivalent. Why am I thinking wrong? A second related question: The line char *ap = a; points to the first element of array a. I used char *ap = &a[4]; to point to the 5th element of array a. Is the expression char *ap = a; same as the expression char *ap = &a[0]; Is the last expression only more verbose than the previous one? Thanks a lot...

    Read the article

  • Storing member function pointers of derived classes in map

    - by Kiran Mohan
    Hello, I am trying to implement a factory for two classes Circle, Square both of which inherits from Shape. class Shape { public: virtual static Shape * getInstance() = 0; }; class Circle : public Shape { public: static const std::string type; Shape * getInstance() { return new Circle; } }; const std::string Circle::type = "Circle"; class Square : public Shape { public: static const std::string type; Shape * getInstance() { return new Square; } }; const std::string Square::type = "Square"; I want to now create a map with key as shape type (string) and value as a function pointer to getInstance() of the corresponding derived class. Is it possible? Thanks, Kiran

    Read the article

  • Question about member function pointers in a heirarchy

    - by Jesse Beder
    I'm using a library that defines an interface: template<class desttype> void connect(desttype* pclass, void (desttype::*pmemfun)()); and I have a small heirarchy class base { void foo(); }; class derived: public base { ... }; In a member function of derived, I want to call connect(this, &derived::foo); but it seems that &derived::foo is actually a member function pointer of base; gcc spits out error: no matching function for call to ‘connect(derived* const&, void (base::* const&)())’ I can get around this by explicitly casting this to base *; but why can't the compiler match the call with desttype = base (since derived * can be implicitly cast to base *)? Also, why is &derived::foo not a member function pointer of derived?

    Read the article

  • Throwing Exception in CTOR and Smart Pointers

    - by David Relihan
    Is it OK to have the following code in my constructor to load an XML document into a member variable - throwing to caller if there are any problems: MSXML2::IXMLDOMDocumentPtr m_docPtr; //member Configuration() { try { HRESULT hr = m_docPtr.CreateInstance(__uuidof(MSXML2::DOMDocument40)); if ( SUCCEEDED(hr)) { m_docPtr->loadXML(CreateXML()); } else { //throw exception to caller } } catch(...) { //throw exception to caller } } Based on Scott Myers RAII implementations in More Effective C++ I believe I am alright in just allowing exceptions to be thrown from CTOR as I am using a smart pointer(IXMLDOMDocumentPtr). Let me know what you think....

    Read the article

  • COM Pointers and process termination

    - by Tony
    Can an unreleased COM pointer to an external process (still alive) cause that process to hang on destruction? Even with TerminateProcess called on it? Process A has a COM interface pointer reference to Process B, now Process B issues a TerminateProcess on A, if some COM interface pointer to Process B in Process A is not released properly, could it be that the process hangs on termination?

    Read the article

  • Pointers in C# to make int array?

    - by Joshua
    The following C++ program compiles and runs as expected: #include <stdio.h> int main(int argc, char* argv[]) { int* test = new int[10]; for (int i = 0; i < 10; i++) test[i] = i * 10; printf("%d \n", test[5]); // 50 printf("%d \n", 5[test]); // 50 return getchar(); } The closest C# simple example I could make for this question is: using System; class Program { unsafe static int Main(string[] args) { // error CS0029: Cannot implicitly convert type 'int[]' to 'int*' int* test = new int[10]; for (int i = 0; i < 10; i++) test[i] = i * 10; Console.WriteLine(test[5]); // 50 Console.WriteLine(5[test]); // Error return (int)Console.ReadKey().Key; } } So how do I make the pointer?

    Read the article

  • STL Vectors, pointers and classes

    - by anubis9
    Hey! Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1(); ~Class1() { //iterate over all the members of the vector and delete the objects } }; class InitializerClass2 { private: Class1 * mPtrToClass1; public: InitializerClass2(); void Initialize() { mPtrToClass1->mVec.push_back(new CustomClass3(bla bla parameters)); } }; Will this work? Or the memory allocated in the InitializerClass2::Initialize() method might get corrupted after the method terminates? Thanks!

    Read the article

  • Vectors of Pointers, inheritance

    - by user308553
    Hi I am a C++ beginner just encountered a problem I don't know how to fix I have two class, this is the header file: class A { public: int i; A(int a); }; class B: public A { public: string str; B(int a, string b); }; then I want to create a vector in main which store either class A or class B vector<A*> vec; A objectOne(1); B objectTwo(2, "hi"); vec.push_back(&objectOne); vec.push_back(&objectTwo); cout << vec.at(1)->i; //this is fine cout << vec.at(1)->str; //ERROR here I am really confused, I checked sites and stuff but I just don't know how to fix it, please help thanks in advance

    Read the article

  • Copy constructor, objects, pointers

    - by Pauff
    Let's say I have this: SolutionSet(const SolutionSet &solutionSet) { this->capacity_ = solutionSet.capacity_; this->solutionsList_ = solutionSet.solutionsList_; // <-- } And solutionsList_ is a vector<SomeType*> vect*. What is the correct way to copy that vector (I suppose that way I'm not doing it right..)?

    Read the article

  • An array of LPWSTR pointers, not working right.

    - by BigBirdy
    Declare: LPWSTR** lines= new LPWSTR*[totalLines]; then i set using: lines[totalLines]=&totalText; SetWindowText(totalChat,(LPWSTR)lines[totalLines]); totalLines++; Now I know totalText is right, cause if i SetWindowText using totalText it works fine. I need the text in totalLines too. I'm also doing: //accolating more memory. int orgSize=size; LPWSTR** tempArray; if (totalLines == size) { size *= 2; tempArray = new LPWSTR*[size]; memcpy(tempArray, lines,sizeof(LPWSTR)*orgSize); delete [] lines; lines = tempArray; } to allocate more memory when needed. My problem is that the lines is not getting the right data. It works for the first time around then it get corrupted. I thought at first i was overwriting but totalLines is increase. Hopefully this is enough information.

    Read the article

  • Function pointers in Objective-C

    - by Stefan Klumpp
    I have the following scenario: Class_A - method_U - method_V - method_X - method_Y Class_B - method_M - method_N HttpClass - startRequest - didReceiveResponse // is a callback Now I want to realize these three flows (actually there are many more, but these are enough to demonstrate my question): Class_A :: method_X -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_A :: method_Y:result and: Class_A :: method_U -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_A :: method_V:result and the last one: Class_B :: method_M -> HttpClass :: startRequest:params -> ... wait, wait, wait ... -> HttpClass :: didReceiveResponse -> Class_B :: method_N:result Please note, that the methods in Class_A and Class_B have different names and functionality, they just make us of the same HttpClass. My solution now would be to pass a C function pointer to startRequest, store it in the HttpClass and when didReceiveResponse gets called I invoke the function pointer and pass the result (which will always be a JSON Dictionary). Now I'm wondering if there can be any problems using plain C or if there are better solutions doing it in a more Objective-C way. Any ideas?

    Read the article

  • Problem with pointers

    - by noname
    OK, i have a strange problem. I have this piece of code: int *p; int test; p=&test; In Visual C++ express, in my exsisting project, I get this error: missing type specifier - int assumed. 'p' : 'int' differs in levels of indirection from 'char *' 'initializing' : cannot convert from 'char *' to 'int' But when i create new project, same code is fine. Whats the problem please?

    Read the article

  • For-Each and Pointers in Java

    - by John
    Ok, so I'm tyring to iterate through an ArrayList and remove a specefic element. However, I am having some trouble using the For-Each like structure. When I run the following code: ArrayList<String> arr = new ArrayList<String>(); //... fill with some values (doesn't really matter) for(String t : arr) { t = " some other value "; //hoping this would change the actual array } for(String t : arr) { System.out.println(t); //however, I still get the same array here } My question in, how can I make 't' a pointer to 'arr' so that I am able to change the values in a for-each loop? I know I could loop through the ArrayList using a different structure, but this one looks so clean and readable, it would just be nice to be able to make 't' a pointer. All comments are appreciated! Even if you say I should just suck it up and use a different construct.

    Read the article

  • Using pointers to adjust global objects in objective-c

    - by Rob
    Ok, so I am working with two sets of data that are extremely similar, and at the same time, these data sets are both global NSMutableArrays within the object. data_set_one = [[NSMutableArray alloc] init]; data_set_two = [[NSMutableArray alloc] init]; Two new NSMutableArrays are loaded, which need to be added to the old, existing data. These Arrays are also global. xml_dataset_one = [[NSMutableArray alloc] init]; xml_dataset_two = [[NSMutableArray alloc] init]; To reduce code duplication (and because these data sets are so similar) I wrote a void method within the class to handle the data combination process for both Arrays: -(void)constructData:(NSMutableArray *)data fromDownloadArray:(NSMutableArray *)down withMatchSelector:(NSString *)sel_str Now, I have a decent understanding of object oriented programming, so I was thinking that if I were to invoke the method with the global Arrays in the data like so... [self constructData:data_set_one fromDownloadArray:xml_dataset_one withMatchSelector:@"id"]; Then the global NSMutableArrays (data_set_one) would reflect the changes that happen to "array" within the method. Sadly, this is not the case, data_set_one doesn't reflect the changes (ex: new objects within the Array) outside of the method. Here is a code snippet of the problem // data_set_one is empty // xml_dataset_one has a few objects [constructData:(NSMutableArray *)data_set_one fromDownloadArray:(NSMutableArray *)xml_dataset_one withMatchSelector:(NSString *)@"id"]; // data_set_one should now be xml_dataset_one, but when echoed to screen, it appears to remain empty And here is the gist of the code for the method, any help is appreciated. -(void)constructData:(NSMutableArray *)data fromDownloadArray:(NSMutableArray *)down withMatchSelector:(NSString *)sel_str { if ([data count] == 0) { data = down; // set data equal to downloaded data } else if ([down count] == 0) { // download yields no results, do nothing } else { // combine the two arrays here } } This project is not ARC enabled. Thanks for the help guys! Rob

    Read the article

  • C++: Trouble with Pointers, loop variables, and structs

    - by Rosarch
    Consider the following example: #include <iostream> #include <sstream> #include <vector> #include <wchar.h> #include <stdlib.h> using namespace std; struct odp { int f; wchar_t* pstr; }; int main() { vector<odp> vec; ostringstream ss; wchar_t base[5]; wcscpy_s(base, L"1234"); for (int i = 0; i < 4; i++) { odp foo; foo.f = i; wchar_t loopStr[1]; foo.pstr = loopStr; // wchar_t* = wchar_t ? Why does this work? foo.pstr[0] = base[i]; vec.push_back(foo); } for (vector<odp>::iterator iter = vec.begin(); iter != vec.end(); iter++) { cout << "Vec contains: " << iter->f << ", " << *(iter->pstr) << endl; } } This produces: Vec contains: 0, 52 Vec contains: 1, 52 Vec contains: 2, 52 Vec contains: 3, 52 I would hope that each time, iter->f and iter->pstr would yield a different result. Unfortunately, iter->pstr is always the same. My suspicion is that each time through the loop, a new loopStr is created. Instead of copying it into the struct, I'm only copying a pointer. The location that the pointer writes to is getting overwritten. How can I avoid this? Is it possible to solve this problem without allocating memory on the heap?

    Read the article

  • Returning pointers in a thread-safe way.

    - by Roddy
    Assume I have a thread-safe collection of Things (call it a ThingList), and I want to add the following function. Thing * ThingList::findByName(string name) { return &item[name]; // or something similar.. } But by doing this, I've delegated the responsibility for thread safety to the calling code, which would have to do something like this: try { list.lock(); // NEEDED FOR THREAD SAFETY Thing *foo = list.findByName("wibble"); foo->Bar = 123; list.unlock(); } catch (...) { list.unlock(); throw; } Obviously a RAII lock/unlock object would simplify/remove the try/catch/unlocks, but it's still easy for the caller to forget. There are a few alternatives I've looked at: Return Thing by value, instead of a pointer - fine unless you need to modify the Thing Add function ThingList::setItemBar(string name, int value) - fine, but these tend to proliferate Return a pointerlike object which locks the list on creation and unlocks it again on destruction. Not sure if this is good/bad practice... What's the right approach to dealing with this?

    Read the article

  • C++ containers on classes, returning pointers

    - by otneil
    Hello, I'm having some trouble to find the best way to accomplish what I have in mind due to my inexperience. I have a class where I need to a vector of objects. So my first question will be: is there any problem having this: vector< AnyType container* and then on the constructor initialize it with new (and deleting it on the destructor)? Another question is: if this vector is going to store objects, shouldn't it be more like vector< AnyTipe* so they could be dynamically created? In that case how would I return an object from a method and how to avoid memory leaks (trying to use only STL)?

    Read the article

< Previous Page | 4 5 6 7 8 9 10 11 12 13 14 15  | Next Page >