logo
down
shadow

how to dereference a pointer of a map of pointers to objects in c++


how to dereference a pointer of a map of pointers to objects in c++

By : Henry Stukenborg
Date : November 18 2020, 04:01 AM
this one helps. I am just reviewing.
code :
... (*_employeePayroll)["Karl"]->employeeID ...
...
itEmployeeID = _employeePayroll->find("Karl");
if ( itEmployeeID != _employeePayroll->end() )
{
    ... (itEmployeeID->second)->employeeID ...
#include    <iostream>
#include    <string>
#include    <map>

class Employee
{
public:
    int     employeeID;

    Employee()
    {
        employeeID = 123;
    }
};

int main(int argc, char* argv[]) {
    std::map<std::string, Employee *>                   *_employeePayroll;
    std::map<std::string, Employee *>                   _employeeID;
    std::map<std::string, Employee *>::const_iterator   itEmployeeID;

    _employeePayroll = &_employeeID;
    (*_employeePayroll)["Karl"] = new Employee;

    itEmployeeID = _employeePayroll->find("Karl");
    if ( itEmployeeID != _employeePayroll->end() )
    {
        std::cout << (itEmployeeID->second)->employeeID;
        std::cout << std::endl;
    }

    return 0;
}
#include    <iostream>
#include    <string>
#include    <map>

class Employee
{
public:
    int     employeeID;

    Employee()
    {
        employeeID = 123;
    }
};

int main(int argc, char* argv[]) {
    std::map<std::string, Employee *> *_employeePayroll;
    std::map<std::string, Employee *> _employeeID;
    _employeePayroll = &_employeeID;

    int iValue;

    (*_employeePayroll)["Karl"] = new Employee;
    iValue = (*_employeePayroll)["Karl"]->employeeID;
    std::cout << iValue;
    std::cout << std::endl;

    return 0;
}


Share : facebook icon twitter icon
Dereference of null pointer, but I am not using pointers

Dereference of null pointer, but I am not using pointers


By : Iqra Farheen
Date : March 29 2020, 07:55 AM
will be helpful for those in need The first if statement in your init method is checking whether or not [super init] returns nil. (Technically it should be written if ((self = [super init])), which the new LLVM compiler will warn you about).
The static analyser is checking ALL possible code paths, even the case where [super init] returns nil. In this case, your if statement fails and self is nil. If self is nil then its instance variables aren't accessible.
I could not figure out how to dereference this pointer for a sizeof() and void pointers

I could not figure out how to dereference this pointer for a sizeof() and void pointers


By : ProphecyX
Date : March 29 2020, 07:55 AM
this will help sizeof is compile-time constant that works on exact type you provide. sizeof of void* is just a size of pointer on your machine (likely 4/8 bytes). Just store size along with other data.
Segmentation fault trying to dereference a pointer from a vector of pointers

Segmentation fault trying to dereference a pointer from a vector of pointers


By : user3605960
Date : March 29 2020, 07:55 AM
I wish this helpful for you In my opinion the vector elements are badly initialized. Perhaps you have to test the code that fill the vector independently before testing to extract them. Sorry for my english ;)
Pointers: Read access violation when trying to dereference pointer by adding a large number

Pointers: Read access violation when trying to dereference pointer by adding a large number


By : Lee Dallas
Date : March 29 2020, 07:55 AM
wish of those help arth is pointing to a single instance of an int. Adding any non-zero value to arth and subsequently dereferencing that new pointer value reads a memory location outside the bounds of the original int. This invokes undefined behavior.
With undefined behavior, anything can happen. Your program may crash, it may output strange results, or it may appear to work properly. The fact that you didn't crash on the *(arth + 1)) case but did crash on the *(arth + 1000)) is an example of that.
How to overload the arrow dereference operator->() not for solid objects but for pointers

How to overload the arrow dereference operator->() not for solid objects but for pointers


By : Hendrik Witarsa
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You cannot replace -> on a pointer.
You can create a smart pointer1 that overloads ->, but built-in operators may not be overloaded.
code :
auto m = DatabaseManager::instance();
m->Query(...); // db->Query 
class DatabaseManager {
  struct DatabaseManagerPtr {
    DatabaseManager* ptr;
    sqdb::Db * operator->() {
      return ptr?ptr->db:nullptr;
    }
  };
  friend struct DatabaseManagerPtr;
private:
  static DatabaseManager * sharedInstance_;
  sqdb::Db *db = nullptr;

public:
  static DatabaseManagerPtr instance() {
    return {sharedInstance_};
  }
};
Related Posts Related Posts :
  • C++ on Vistual Studio with CMake error: manifest 'build.ninja' still dirty after 100 tries
  • Is there a way to automatically resolve an overloaded method via a template parameter?
  • Fastest way to find pair in a vector, remove it while iterating
  • error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR'
  • Unable to call boost::clear_vertex while using listS for the vertex and edge lists
  • Decoding binary data from serial port
  • Pattern to Implement an OO interface to a C program written in an imperative style
  • CEPH + Librados++ is not returning consisten results
  • `LoadLibraryExW` triggers exception `0xC0000023` from `NtMapViewOfSection`
  • static_cast to a struct type to access all of its member variable
  • Forward declaration for boost::intrusive_ptr class to reduce compile time
  • How to use priority_queue with a non-static compare method of class instance?
  • Template parameters inside and outside class/struct
  • Determining prime number
  • How to resolve ambiguity between constructors taking std::string and std::vector
  • My program crashes when I try to change private values from an object
  • Unordered_map with custom class as key
  • Strict aliasing rules broken with templates and inheritance
  • C++ Derived Class Override Return Type
  • singly linked list c++ constructor, destructor and printing out
  • How to clone class with vector of unique_ptr to base class
  • error: no match for operator
  • std::vector doesnt accept my struct as template
  • selection of people's contours
  • how to fix the (Error using mexOpenCV) on matlab?
  • Is or was there a proposal for c++ to use the context for short enum values?
  • Fair assumptions about std::hash implementations
  • undefined reference to libusb using cyusb
  • Function returns null pointer instead of address
  • C++17 copy elision and object destruction
  • Input multiple strings via operator>> c++
  • Avoiding overflow boost container
  • How to Write a Lambda Wrapping a Function with Optional Return Value
  • Partial specialization with more template parameters
  • How to convert fixed size array to pointer on pointer array
  • Memory leak in const member constructor with tag dispatching
  • C++ function with a generic and optional class
  • Custom QGraphicsItem That Contains Child QGraphicsItems
  • Are There Restrictions on What can be Passed to auto Template Parameters?
  • Rotating line inside rectangle bounds
  • Why do I need dynamic memory allocation if I can just create an array?
  • How can I convert a text file into a form that MPI_Bcast can send?
  • How to get array of all `this` of an instance
  • Using pointers as parameters
  • Automatic type deduction with const_cast is not working
  • Why does std::is_rvalue_reference not do what it is advertised to do?
  • Function Template Specialization with Forward Declared Type
  • template deduction failed in vector
  • Is there a signed `sizeof` alternative in Qt
  • clarification on overloading the -> operator
  • What is __m128d?
  • QtConcurrent: why releaseThread and reserveThread cause deadlock?
  • Function receiving different value than passed
  • Can C++ close a '''fstream''' variable after '''.close()'''?
  • Is it necessary to overload operator in this specific case
  • Comparing an element of a string array with a string
  • How recursive function control flow change for static variable?
  • SDL 2 blitting BMP File
  • Why does an extremely large value cause this code to repeat infinitely?
  • Function returning different value than what is in function body before return statement
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org