Questions tagged [object-slicing]

Object slicing refers to assignment by value of a sub-class instance to a super-class instance,thereby losing part of the information i.e. the sub-class specific data members are ignored.

220 questions
1
vote
3 answers

Average over time series data with custom time slice window

I am working with some tennis ranking data in R, that gives the evolution of tennis rankings over time of all the players on the ATP tour. An example of the data I am using can be found here, giving the rankings data from 2000's:…
ap21
  • 2,372
  • 3
  • 16
  • 32
1
vote
1 answer

Why is my exception sliced to base class if I catch it with reference to base class?

So I've written a small C++ class as follows: class bad_hmean : public std::logic_error { const char *nature_; char *what_; public: bad_hmean(const char *fname); ~bad_hmean() { delete[] what_; } const char *what() { return…
1
vote
2 answers

Limit on Number of cart-products

I'd like to set a limit on the number of products that can be added on a cart. --Case scenario: Assuming delivery-resource scarcity, I don't want users to add more than 5 products at a time. (The quantity of same product can, however, be…
Magere
  • 99
  • 6
1
vote
7 answers

set the base object of derived object?

This is a basic concept question. If I have a class that Derived that inherits from Base, and I instantiate a new Derived object, can I set it's Base object to a specific Base object of my choosing so that all calls base class methods are redirected…
user487100
  • 918
  • 1
  • 11
  • 22
1
vote
7 answers

Removing set of characters from the end of the string

I want to remove @email.com from this string and also as such that even if the "myemail" part is changed the code should still work I did this: email = "myemail@email.com" a = a.replace(a[-10:-1],'') print(a) Output: myemailm I wanted to remove…
Harsh
  • 126
  • 6
1
vote
2 answers

Return reference to sliced object (supertype)

Consider the following classes: class Coord { public: double _x, _y; Coord(double x, double y) { _x = x; _y = y; } }; class NamedPoint : public Coord { public: int _id; NamedPoint(int id, double x, double…
jedwards
  • 29,432
  • 3
  • 65
  • 92
1
vote
1 answer

what does k[:0] means, if k is an empty list

I want to know what does list[:0] means. In the following code what it is doing? LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' k=[] while len(k) < 20: for i in LETTERS: k[:0] = i print(k) # k = ['Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R',…
1
vote
1 answer

Slicing pandas dataframe based on rearranged duplicates (or how to drop rearranged duplicates)

I have a big dataframe with the format col1 col2 val1 val2 [0]A B 0.8 0.1 [1]B A 0.8 0.1 [2]A C 0.3 0.9 [3]A D 0.2 0.8 [4]D A 0.2 0.8 As you can see, some…
Sos
  • 1,783
  • 2
  • 20
  • 46
1
vote
0 answers

Equivalent to R " -c()" syntax to exclude rows / columns for Python numpy array

I'm trying to find an efficient way to explicitly exclude a list of columns from a numpy array. I'm aware that in R by placing the "minus" sing before c() you can specify what columns not to include from a data frame I have tried using "~" before…
user11449055
1
vote
2 answers

Virtual Table and Object Slicing

In object slicing, when a derived class object is copied to a Base class object, does the _vptr of Derived class also get copied to _vptr of Base class, like other members of class Base? If not, why? class Base { public : virtual void Display()…
1
vote
3 answers

How to do python style string slicing in c++

Is it possible to implement a method through which I can do slicing in C++ using : operator. For example,I define a C-style string as shown below: char my_name[10] {"InAFlash"}; Can I implement a function or override any internal method to do the…
rawwar
  • 4,834
  • 9
  • 32
  • 57
1
vote
1 answer

Array of Pointers to Different Derived Classes

I'm working on the core for a family of microprocessor units based off of the ATMEGA328P. I have a base class and two derived classes that I need to store mixed together in an array. I have the following: // base class class Channel { virtual…
Phillip McMullen
  • 453
  • 2
  • 5
  • 15
1
vote
3 answers

C++ Constructor member initializer lists, Object Slicing

I have two classes class A { public: virtual void doStuff() = 0; }; class B : public A { int x; public: virtual void doStuff() override { x = x*2;} //just example function }; And another class that modify and use data from the…
1
vote
2 answers

String slicing big O

for i in range(n): print("HelloWorld"[i:]) Is this O(n) or should I count the slicing as running over the characters of "HelloWorld"? Also, when I compare two strings s1==s2 does this operation run over each letter in s1 and compare it with…
1
vote
2 answers

Member variables and object slicing

This answer suggests that object slicing in vectors can be overcome with the use of pointers. From my testing, this is true when working with variables shared between the base and derived classes. For example, given these classes: class Base…
Nullsrc
  • 33
  • 7